diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 0dda180..7a276be 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -2961,9 +2961,9 @@ keep_going:						/* We will come back to here until there is
 					char	   *val;
 
 					val = PQgetvalue(res, 0, 0);
+					PQclear(res);
 					if (strncmp(val, "on", 2) == 0)
 					{
-						PQclear(res);
 						restoreErrorMessage(conn, &savedMessage);
 
 						/* Not writable; close connection. */
@@ -2988,15 +2988,22 @@ keep_going:						/* We will come back to here until there is
 						/* No more addresses to try. So we fail. */
 						goto error_return;
 					}
-					PQclear(res);
-					termPQExpBuffer(&savedMessage);
 
-					/* We can release the address lists now. */
-					release_all_addrinfo(conn);
+					/*
+					 * Call PQgetResult() again to consume NULL result.
+					 * Anything else is an error and will be dealt with below.
+					 */
+					if ((res = PQgetResult(conn)) == NULL)
+					{
+						termPQExpBuffer(&savedMessage);
 
-					/* We are open for business! */
-					conn->status = CONNECTION_OK;
-					return PGRES_POLLING_OK;
+						/* We can release the address lists now. */
+						release_all_addrinfo(conn);
+
+						/* We are open for business! */
+						conn->status = CONNECTION_OK;
+						return PGRES_POLLING_OK;
+					}
 				}
 
 				/*
