     ///
     /// - Returns: true if the data read in was processed
     func handleRead() -> Bool {
-        guard isOpen && socket.socketfd > -1 else { return true }
+        handleReadInProgress = true
+        defer {
+            handleReadInProgress = false // needs to be unset before calling close() as it is part of the guard in close()
+            if preparingToClose {
+                close()
+            }
+        }
+
+        // Set handleReadInProgress flag to true before the guard below to avoid another thread
+        // invoking close() in between us clearing the guard and setting the flag.
+        guard isOpen && socket.socketfd > -1 else {
+            preparingToClose = true // flag the function defer clause to cleanup if needed
+            return true
+        }
 
         var result = true
         
         do {
             var length = 1
-            readInProgress = true
             while  length > 0  {
                 length = try socket.read(into: readBuffer)
             }
-            readInProgress = false
             if  readBuffer.length > 0  {
                 result = handleReadHelper()
             }

                if socket.remoteConnectionClosed  {
                    Log.debug("socket remoteConnectionClosed in handleRead()")
                    processor?.socketClosed()
                    prepareToClose()
                }
            }
        }
        catch let error as Socket.Error {
            readInProgress = false
            if error.errorCode == Int32(Socket.SOCKET_ERR_CONNECTION_RESET) {
                Log.debug("Read from socket (file descriptor \(socket.socketfd)) reset. Error = \(error).")
            } else {
                Log.error("Read from socket (file descriptor \(socket.socketfd)) failed. Error = \(error).")
            }
            prepareToClose()
        } catch {
            readInProgress = false
            Log.error("Unexpected error...")
            prepareToClose()
        }
        return result
    }
    

                }
            }
            catch let error {
                if let error = error as? Socket.Error, error.errorCode == Int32(Socket.SOCKET_ERR_CONNECTION_RESET) {
                    Log.debug("Write to socket (file descriptor \(socket.socketfd)) failed. Error = \(error).")
                } else {
