Index: /trunk/src/VBox/Runtime/include/internal/socket.h
===================================================================
--- /trunk/src/VBox/Runtime/include/internal/socket.h	(revision 27548)
+++ /trunk/src/VBox/Runtime/include/internal/socket.h	(revision 27549)
@@ -37,15 +37,24 @@
 
 
+/** Native socket handle type. */
+#ifdef RT_OS_WINDOWS
+# define RTSOCKETNATIVE         SOCKET
+#else
+# define RTSOCKETNATIVE         int
+#endif
+
+/** NIL value for native socket handles. */
+#ifdef RT_OS_WINDOWS
+# define NIL_RTSOCKETNATIVE     INVALID_SOCKET
+#else
+# define NIL_RTSOCKETNATIVE     (-1)
+#endif
+
+
 RT_C_DECLS_BEGIN
 
 #ifndef IPRT_INTERNAL_SOCKET_POLLING_ONLY
 int rtSocketResolverError(void);
-int rtSocketCreateForNative(RTSOCKETINT **ppSocket,
-# ifdef RT_OS_WINDOWS
-                            SOCKET hNative
-# else
-                            int hNative
-# endif
-                            );
+int rtSocketCreateForNative(RTSOCKETINT **ppSocket, RTSOCKETNATIVE hNative);
 int rtSocketCreate(PRTSOCKET phSocket, int iDomain, int iType, int iProtocol);
 int rtSocketBind(RTSOCKET hSocket, const struct sockaddr *pAddr, int cbAddr);
Index: /trunk/src/VBox/Runtime/r3/socket.cpp
===================================================================
--- /trunk/src/VBox/Runtime/r3/socket.cpp	(revision 27548)
+++ /trunk/src/VBox/Runtime/r3/socket.cpp	(revision 27549)
@@ -124,7 +124,7 @@
      *  handle concurrently. */
     uint32_t volatile   cUsers;
-#ifdef RT_OS_WINDOWS
     /** The native socket handle. */
-    SOCKET              hNative;
+    RTSOCKETNATIVE      hNative;
+#ifdef RT_OS_WINDOWS
     /** The event semaphore we've associated with the socket handle.
      * This is WSA_INVALID_EVENT if not done. */
@@ -138,8 +138,5 @@
      * This is ZERO if we're currently not subscribing to anything. */
     uint32_t            fSubscribedEvts;
-#else
-    /** The native socket handle. */
-    int                 hNative;
-#endif
+#endif /* RT_OS_WINDOWS */
 } RTSOCKETINT;
 
@@ -246,11 +243,5 @@
  * @param   hNative         The native handle.
  */
-int rtSocketCreateForNative(RTSOCKETINT **ppSocket,
-#ifdef RT_OS_WINDOWS
-                            SOCKET hNative
-#else
-                            int hNative
-#endif
-                            )
+int rtSocketCreateForNative(RTSOCKETINT **ppSocket, RTSOCKETNATIVE hNative)
 {
     RTSOCKETINT *pThis = (RTSOCKETINT *)RTMemAlloc(sizeof(*pThis));
@@ -286,13 +277,7 @@
      * Create the socket.
      */
-#ifdef RT_OS_WINDOWS
-    SOCKET  hNative = socket(iDomain, iType, iProtocol);
-    if (hNative == INVALID_SOCKET)
+    RTSOCKETNATIVE hNative = socket(iDomain, iType, iProtocol);
+    if (hNative == NIL_RTSOCKETNATIVE)
         return rtSocketError();
-#else
-    int     hNative = socket(iDomain, iType, iProtocol);
-    if (hNative == -1)
-        return rtSocketError();
-#endif
 
     /*
@@ -333,29 +318,23 @@
         pThis->hEvent = WSA_INVALID_EVENT;
     }
-
-    if (pThis->hNative != INVALID_SOCKET)
-    {
-        rc = closesocket(pThis->hNative);
-        if (!rc)
-            rc = VINF_SUCCESS;
-        else
+#endif
+
+    if (pThis->hNative != NIL_RTSOCKETNATIVE)
+    {
+#ifdef RT_OS_WINDOWS
+        if (closesocket(pThis->hNative))
+#else
+        if (close(pThis->hNative))
+#endif
         {
             rc = rtSocketError();
-            AssertMsgFailed(("\"%s\": closesocket(%p) -> %Rrc\n", pThis->hNative, rc));
+#ifdef RT_OS_WINDOWS
+            AssertMsgFailed(("\"%s\": closesocket(%p) -> %Rrc\n", (uintptr_t)pThis->hNative, rc));
+#else
+            AssertMsgFailed(("\"%s\": close(%d) -> %Rrc\n", pThis->hNative, rc));
+#endif
         }
-        pThis->hNative = INVALID_SOCKET;
-    }
-
-#else
-    if (pThis->hNative != -1)
-    {
-        if (close(pThis->hNative))
-        {
-            rc = rtSocketError();
-            AssertMsgFailed(("\"%s\": close(%d) -> %Rrc\n", pThis->hNative, rc));
-        }
-        pThis->hNative = -1;
-    }
-#endif
+        pThis->hNative = NIL_RTSOCKETNATIVE;
+    }
 
     return rc;
@@ -569,5 +548,6 @@
 {
     /*
-     * Validate input.
+     * Validate input, don't lock it because we might want to interrupt a call
+     * active on a different thread.
      */
     RTSOCKETINT *pThis = hSocket;
@@ -575,5 +555,4 @@
     AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
     AssertReturn(fRead || fWrite, VERR_INVALID_PARAMETER);
-    //AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
 
     /*
@@ -591,5 +570,4 @@
         rc = rtSocketError();
 
-    rtSocketUnlock(pThis);
     return rc;
 }
@@ -770,9 +748,15 @@
     /*
      * Validate input.
-     */
-    RTSOCKETINT *pThis = hSocket;
-    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
-    AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
+     * Only lock the socket temporarily while we get the native handle, so that
+     * we can safely shutdown and destroy the socket from a different thread.
+     */
+    RTSOCKETINT *pThis = hSocket;
+    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
+    AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
+
     AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
+
+    rtSocketUnlock(pThis);
+
 
     /*
@@ -783,11 +767,9 @@
 #ifdef RT_OS_WINDOWS
     int         cbAddr  = (int)*pcbAddr;
-    SOCKET      hNative = accept(pThis->hNative, pAddr, &cbAddr);
-    if (hNative != INVALID_SOCKET)
 #else
     socklen_t   cbAddr  = *pcbAddr;
-    int         hNative = accept(pThis->hNative, pAddr, &cbAddr);
-    if (hNative != -1)
-#endif
+#endif
+    RTSOCKETNATIVE hNative = accept(pThis->hNative, pAddr, &cbAddr);
+    if (hNative != NIL_RTSOCKETNATIVE)
     {
         *pcbAddr = cbAddr;
@@ -808,6 +790,4 @@
     else
         rc = rtSocketError();
-
-    rtSocketUnlock(pThis);
     return rc;
 }
