*** a/src/interfaces/libpq/fe-connect.c
--- b/src/interfaces/libpq/fe-connect.c
***************
*** 38,43 ****
--- 38,44 ----
  #endif
  #define near
  #include <shlobj.h>
+ #include <mstcpip.h>
  #else
  #include <sys/socket.h>
  #include <netdb.h>
***************
*** 982,987 **** useKeepalives(PGconn *conn)
--- 983,989 ----
  	return val != 0 ? 1 : 0;
  }
  
+ #ifndef WIN32
  /*
   * Set the keepalive idle timer.
   */
***************
*** 1076,1081 **** setKeepalivesCount(PGconn *conn)
--- 1078,1150 ----
  	return 1;
  }
  
+ #else /* Win32 */
+ /*
+  * Enable keepalives and set the keepalive values on Win32,
+  * where they are always set in one batch.
+  */
+ static int
+ setKeepalivesWin32(PGconn *conn)
+ {
+ 	struct tcp_keepalive 	ka;
+ 	DWORD					retsize;
+ 	int						idle = 0;
+ 	int						interval = 0;
+ 
+ 	if (conn->keepalives_idle == NULL &&
+ 		conn->keepalives_interval == NULL)
+ 	{
+ 		/*
+ 		 * When neither is specified, use the setsockopt() API to avoid
+ 		 * overwriting the system default values.
+ 		 */
+ 		int		on = 1;
+ 		char	sebuf[256];
+ 
+ 		if (setsockopt(conn->sock,
+ 					   SOL_SOCKET, SO_KEEPALIVE,
+ 					   (char *) &on, sizeof(on)) < 0)
+ 		{
+ 			appendPQExpBuffer(&conn->errorMessage,
+ 							  libpq_gettext("setsockopt(SO_KEEPALIVE) failed: %s\n"),
+ 							  SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
+ 			return 0;
+ 		}
+ 	}
+ 
+ 	if (conn->keepalives_idle)
+ 		idle = atoi(conn->keepalives_idle);
+ 	if (idle <= 0)
+ 		idle = 2 * 60 * 60; /* 2 hours = default */
+ 
+ 	if (conn->keepalives_interval)
+ 		interval = atoi(conn->keepalives_interval);
+ 	if (interval <= 0)
+ 		interval = 1; /* 1 second = default */
+ 
+ 	ka.onoff = 1;
+ 	ka.keepalivetime = idle * 1000;
+ 	ka.keepaliveinterval = interval * 1000;
+ 
+ 	if (WSAIoctl(conn->sock,
+ 				 SIO_KEEPALIVE_VALS,
+ 				 (LPVOID) &ka,
+ 				 sizeof(ka),
+ 				 NULL,
+ 				 0,
+ 				 &retsize,
+ 				 NULL,
+ 				 NULL)
+ 		!= 0)
+ 	{
+ 		appendPQExpBuffer(&conn->errorMessage,
+ 						  libpq_gettext("WSAIoctl(SIO_KEEPALIVE_VALS) failed: %ui\n"),
+ 						  WSAGetLastError());
+ 		return 0;
+ 	}
+ 	return 1;
+ }
+ #endif /* WIN32 */
  
  /* ----------
   * connectDBStart -
***************
*** 1478,1483 **** keep_going:						/* We will come back to here until there is
--- 1547,1553 ----
  						{
  							/* Do nothing */
  						}
+ #ifndef WIN32
  						else if (setsockopt(conn->sock,
  											SOL_SOCKET, SO_KEEPALIVE,
  											(char *) &on, sizeof(on)) < 0)
***************
*** 1491,1496 **** keep_going:						/* We will come back to here until there is
--- 1561,1570 ----
  								 || !setKeepalivesInterval(conn)
  								 || !setKeepalivesCount(conn))
  							err = 1;
+ #else /* WIN32 */
+ 						else if (!setKeepalivesWin32(conn))
+ 							err = 1;
+ #endif /* WIN32 */
  
  						if (err)
  						{
