diff --git a/lib/client-handshake.c b/lib/client-handshake.c
index 004ed9393cfe07a3631a7213640f03c549a2154f..09e83bdaf1d7fcc6cf3c0a149bac21200b318a07 100644
--- a/lib/client-handshake.c
+++ b/lib/client-handshake.c
@@ -159,8 +159,10 @@ struct libwebsocket *libwebsocket_client_connect_2(
 		wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT;
 
 		lws_libev_accept(context, wsi, wsi->sock);
-		if (insert_wsi_socket_into_fds(context, wsi))
+		if (insert_wsi_socket_into_fds(context, wsi)) {
+			compatible_close(wsi->sock);
 			goto oom4;
+		}
 
 		/*
 		 * past here, we can't simply free the structs as error
@@ -190,14 +192,12 @@ struct libwebsocket *libwebsocket_client_connect_2(
 					(struct sockaddr_in *)v, n) < 0) {
 				lwsl_err("Unable to find interface %s\n",
 								context->iface);
-				compatible_close(wsi->sock);
 				goto failed;
 			}
 
 			if (bind(wsi->sock, v, n) < 0) {
 				lwsl_err("Error binding to interface %s",
 								context->iface);
-				compatible_close(wsi->sock);
 				goto failed;
 			}
 		}
@@ -350,7 +350,7 @@ libwebsocket_client_connect(struct libwebsocket_context *context,
 	if (wsi == NULL)
 		goto bail;
 
-	wsi->sock = -1;
+	wsi->sock = LWS_SOCK_INVALID;
 
 	/* -1 means just use latest supported */
 
diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c
index d1ab61949198f76358c6d839d36d43d4d9d43fa2..792d1485fe3c98dc5565a020d6e746293934d337 100644
--- a/lib/libwebsockets.c
+++ b/lib/libwebsockets.c
@@ -300,7 +300,7 @@ just_kill_connection:
 
 /*	lwsl_info("closing fd=%d\n", wsi->sock); */
 
-	if (!lws_ssl_close(wsi) && wsi->sock >= 0) {
+	if (!lws_ssl_close(wsi) && lws_socket_is_valid(wsi->sock)) {
 		n = shutdown(wsi->sock, SHUT_RDWR);
 		if (n)
 			lwsl_debug("closing: shutdown ret %d\n", LWS_ERRNO);
@@ -308,6 +308,7 @@ just_kill_connection:
 		n = compatible_close(wsi->sock);
 		if (n)
 			lwsl_debug("closing: close ret %d\n", LWS_ERRNO);
+		wsi->sock = LWS_SOCK_INVALID;
 	}
 
 	/* outermost destroy notification for wsi (user_space still intact) */
diff --git a/lib/output.c b/lib/output.c
index 44b2ab7e13c6b3819cae9b3201ecc4566735865d..81d1d5c11b6e198e3448dc3a0b0184f1b47b0dfb 100644
--- a/lib/output.c
+++ b/lib/output.c
@@ -120,7 +120,7 @@ int lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len)
 		n = m;
 		goto handle_truncated_send;
 	}
-	if (wsi->sock < 0)
+	if (!lws_socket_is_valid(wsi->sock))
 		lwsl_warn("** error invalid sock but expected to send\n");
 
 	/*
diff --git a/lib/pollfd.c b/lib/pollfd.c
index a70f8e6407f2a79ed6824b8452bd8cc1be8556a6..04e2e4d3dc1a8707c61b01df581b3ae51e0c03df 100644
--- a/lib/pollfd.c
+++ b/lib/pollfd.c
@@ -41,7 +41,7 @@ insert_wsi_socket_into_fds(struct libwebsocket_context *context,
 #endif
 
 	assert(wsi);
-	assert(wsi->sock >= 0);
+	assert(lws_socket_is_valid(wsi->sock));
 
 	lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n",
 					    wsi, wsi->sock, context->fds_count);
@@ -115,7 +115,7 @@ remove_wsi_socket_from_fds(struct libwebsocket_context *context,
 	wsi->position_in_fds_table = -1;
 
 	/* remove also from external POLL support via protocol 0 */
-	if (wsi->sock) {
+	if (lws_socket_is_valid(wsi->sock)) {
 		if (context->protocols[0].callback(context, wsi,
 		    LWS_CALLBACK_DEL_POLL_FD, wsi->user_space,
 		    (void *) &pa, 0))
diff --git a/lib/server.c b/lib/server.c
index 0b46402f95784298b8406a70571422d5d5846f0f..632a52931d1e0cedca8060048c5fbef0c6326dff 100644
--- a/lib/server.c
+++ b/lib/server.c
@@ -49,7 +49,7 @@ int lws_context_init_server(struct lws_context_creation_info *info,
 #endif
 		sockfd = socket(AF_INET, SOCK_STREAM, 0);
 
-	if (sockfd < 0) {
+	if (sockfd == -1) {
 		lwsl_err("ERROR opening socket\n");
 		return 1;
 	}
@@ -671,7 +671,7 @@ int lws_server_socket_service(struct libwebsocket_context *context,
 			struct libwebsocket *wsi, struct libwebsocket_pollfd *pollfd)
 {
 	struct libwebsocket *new_wsi = NULL;
-	int accept_fd = 0;
+	int accept_fd = LWS_SOCK_INVALID;
 	socklen_t clilen;
 	struct sockaddr_in cli_addr;
 	int n;