client: HUP: defer POLLUP handling while buffered rx

If the server is very close in rtt to the client, the server
hangup may get processed before buffered rx.

Make sure we clear buffered rx before dealing with the HUP.
diff --git a/lib/core-net/service.c b/lib/core-net/service.c
index 080c271..e00bc37 100644
--- a/lib/core-net/service.c
+++ b/lib/core-net/service.c
@@ -689,11 +689,16 @@
 
 	if ((!(pollfd->revents & pollfd->events & LWS_POLLIN)) &&
 	    (pollfd->revents & LWS_POLLHUP)) {
-		wsi->socket_is_permanently_unusable = 1;
-		lwsl_debug("Session Socket %s (fd=%d) dead\n",
-			   lws_wsi_tag(wsi), pollfd->fd);
 
-		goto close_and_handled;
+		if (lws_buflist_total_len(&wsi->buflist))
+			lws_set_timeout(wsi, PENDING_TIMEOUT_CLOSE_ACK, 3);
+		else {
+			wsi->socket_is_permanently_unusable = 1;
+			lwsl_debug("Session Socket %s (fd=%d) dead\n",
+				   lws_wsi_tag(wsi), pollfd->fd);
+
+			goto close_and_handled;
+		}
 	}
 
 #ifdef _WIN32
@@ -701,13 +706,6 @@
 		wsi->sock_send_blocking = FALSE;
 #endif
 
-	if ((!(pollfd->revents & pollfd->events & LWS_POLLIN)) &&
-	    (pollfd->revents & LWS_POLLHUP)) {
-		lwsl_debug("pollhup\n");
-		wsi->socket_is_permanently_unusable = 1;
-		goto close_and_handled;
-	}
-
 #if defined(LWS_WITH_TLS)
 	if (lwsi_state(wsi) == LRS_SHUTDOWN &&
 	    lws_is_ssl(wsi) && wsi->tls.ssl) {