*** a/src/interfaces/libpq/fe-connect.c
--- b/src/interfaces/libpq/fe-connect.c
***************
*** 1321,1326 **** keep_going:						/* We will come back to here until there is
--- 1321,1338 ----
  						continue;
  					}
  #endif   /* F_SETFD */
+ 					optval = 1;
+ 					if (setsockopt(conn->sock, SOL_SOCKET, SO_KEEPALIVE,
+ 						       (char *) &optval, sizeof(optval)) == -1)
+ 					{
+ 						appendPQExpBuffer(&conn->errorMessage,
+ 										  libpq_gettext("could not set keepalive on socket: %s\n"),
+ 							SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
+ 						closesocket(conn->sock);
+ 						conn->sock = -1;
+ 						conn->addr_cur = addr_cur->ai_next;
+ 						continue;
+ 					}
  
  					/*----------
  					 * We have three methods of blocking SIGPIPE during
