Index: trunk/src/wsock32/asyncapi.cpp
===================================================================
--- trunk/src/wsock32/asyncapi.cpp	(revision 7040)
+++ trunk/src/wsock32/asyncapi.cpp	(revision 7041)
@@ -1,3 +1,3 @@
-/* $Id: asyncapi.cpp,v 1.15 2001-07-08 15:44:27 achimha Exp $ */
+/* $Id: asyncapi.cpp,v 1.16 2001-10-13 18:51:07 sandervl Exp $ */
 
 /*
@@ -415,4 +415,5 @@
     {
         dprintf(("AsyncSelectNotifyEvent: notifying event semaphore, socket: 0x%x, HEVENT: 0x%x, event: 0x%x", pThreadParm->u.asyncselect.s, pThreadParm->notifyHandle, event));
+        pThreadParm->u.asyncselect.lLastEvent |= event;
         SetEvent(pThreadParm->notifyHandle);
     }
@@ -442,5 +443,5 @@
 
 	//break if user cancelled request
-      	if(pThreadParm->u.asyncselect.lEvents == 0) {
+      	if(pThreadParm->u.asyncselect.lEvents == 0 || pThreadParm->fCancelled) {
 		break;
       	}
@@ -700,4 +701,5 @@
    pThreadParm->u.asyncselect.lEvents        = lEventMask;
    pThreadParm->u.asyncselect.lEventsPending = lEventMask;
+   pThreadParm->u.asyncselect.lLastEvent     = 0;
    pThreadParm->u.asyncselect.s              = s;
    pThreadParm->u.asyncselect.asyncSem       = new VSemaphore;
@@ -734,10 +736,7 @@
            lpEvent->iErrorCode[i] = 0;
         }
-
-        lpEvent->lNetworkEvents = pThreadInfo->u.asyncselect.lEventsPending;
-
-        // TODO is this correct? API says they're cleared
-        pThreadInfo->u.asyncselect.lEventsPending = 0;
-   } else
+        lpEvent->lNetworkEvents = InterlockedExchange((LPLONG)&pThreadInfo->u.asyncselect.lLastEvent, 0);
+   } 
+   else
    {
       asyncThreadMutex.leave();
@@ -747,4 +746,5 @@
       return SOCKET_ERROR;
    }
+   asyncThreadMutex.leave();
 
    // reset event semaphore if present
@@ -755,6 +755,4 @@
    }
 
-   asyncThreadMutex.leave();
-
    WSASetLastError(NO_ERROR);
    return NO_ERROR;
Index: trunk/src/wsock32/asyncthread.cpp
===================================================================
--- trunk/src/wsock32/asyncthread.cpp	(revision 7040)
+++ trunk/src/wsock32/asyncthread.cpp	(revision 7041)
@@ -1,3 +1,3 @@
-/* $Id: asyncthread.cpp,v 1.11 2001-07-07 14:29:40 achimha Exp $ */
+/* $Id: asyncthread.cpp,v 1.12 2001-10-13 18:51:07 sandervl Exp $ */
 
 /*
@@ -40,4 +40,5 @@
 ////	WSASetBlocking(FALSE, pThreadParm->hThread);
 
+  dprintf(("AsyncThread %x exit", pThreadParm->hAsyncTaskHandle));
   free((PVOID)pThreadParm);
 }
@@ -109,4 +110,25 @@
    memset(pThreadParm, 0, sizeof(*pThreadParm));
    asyncThreadMutex.leave();
+}
+//******************************************************************************
+//******************************************************************************
+void WSACancelAllAsyncRequests()
+{
+ PASYNCTHREADPARM pThreadInfo;
+ BOOL             found = FALSE;
+
+   dprintf(("WSACancelAllAsyncRequests"));
+   asyncThreadMutex.enter();
+   pThreadInfo = threadList;
+
+   while(pThreadInfo) {
+        dprintf(("WSACancelAllAsyncRequests %x", pThreadInfo->hAsyncTaskHandle));
+        pThreadInfo->fCancelled = TRUE;
+        pThreadInfo->u.asyncselect.asyncSem->post();
+	pThreadInfo = pThreadInfo->next;
+   }
+   asyncThreadMutex.leave();
+   //TODO: not the right way to wait for the async threads to die
+   DosSleep(250);
 }
 //******************************************************************************
Index: trunk/src/wsock32/asyncthread.h
===================================================================
--- trunk/src/wsock32/asyncthread.h	(revision 7040)
+++ trunk/src/wsock32/asyncthread.h	(revision 7041)
@@ -1,3 +1,3 @@
-/* $Id: asyncthread.h,v 1.7 2001-07-07 14:29:40 achimha Exp $ */
+/* $Id: asyncthread.h,v 1.8 2001-10-13 18:51:07 sandervl Exp $ */
 
 /*
@@ -71,4 +71,5 @@
 			DWORD       lEvents;
                         DWORD       lEventsPending;
+                        DWORD       lLastEvent;
 			SOCKET      s;
 			int         mode;
@@ -86,5 +87,7 @@
 void  EnableAsyncEvent(SOCKET s, ULONG flags);
 BOOL  QueryAsyncEvent(SOCKET s, HWND *pHwnd, int *pMsg, ULONG *plEvent);
-BOOL FindAndSetAsyncEvent(SOCKET s, int mode, int notifyHandle, int notifyData, ULONG lEventMask);
+BOOL  FindAndSetAsyncEvent(SOCKET s, int mode, int notifyHandle, int notifyData, ULONG lEventMask);
+
+void  WSACancelAllAsyncRequests();
 
 #endif  //__ASYNCTHREAD_H__
Index: trunk/src/wsock32/initwsock32.cpp
===================================================================
--- trunk/src/wsock32/initwsock32.cpp	(revision 7040)
+++ trunk/src/wsock32/initwsock32.cpp	(revision 7041)
@@ -1,3 +1,3 @@
-/* $Id: initwsock32.cpp,v 1.3 2001-09-05 10:26:30 bird Exp $
+/* $Id: initwsock32.cpp,v 1.4 2001-10-13 18:51:08 sandervl Exp $
  *
  * DLL entry point
@@ -46,4 +46,24 @@
 static HMODULE dllHandle = 0;
 
+INT WIN32API WSACleanup();
+//******************************************************************************
+//******************************************************************************
+BOOL WINAPI WinsockLibMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
+{
+   switch (fdwReason)
+   {
+   case DLL_PROCESS_ATTACH:
+    return TRUE;
+
+   case DLL_THREAD_ATTACH:
+   case DLL_THREAD_DETACH:
+    return TRUE;
+
+   case DLL_PROCESS_DETACH:
+    WSACleanup();
+    return TRUE;
+   }
+   return FALSE;
+}
 /****************************************************************************/
 /* _DLL_InitTerm is the function that gets called by the operating system   */
@@ -69,12 +89,12 @@
          ParseLogStatusWSOCK32();
 
-     dllHandle = RegisterLxDll(hModule, NULL, (PVOID)&wsock32_PEResTab);
+         dllHandle = RegisterLxDll(hModule, WinsockLibMain, (PVOID)&wsock32_PEResTab);
          if(dllHandle == 0)
-        return 0UL;
+             return 0UL;
 
          break;
       case 1 :
          if(dllHandle) {
-        UnregisterLxDll(dllHandle);
+             UnregisterLxDll(dllHandle);
          }
          break;
Index: trunk/src/wsock32/wsa.cpp
===================================================================
--- trunk/src/wsock32/wsa.cpp	(revision 7040)
+++ trunk/src/wsock32/wsa.cpp	(revision 7041)
@@ -1,3 +1,3 @@
-/* $Id: wsa.cpp,v 1.6 2000-05-02 13:09:43 bird Exp $ */
+/* $Id: wsa.cpp,v 1.7 2001-10-13 18:51:08 sandervl Exp $ */
 
 /*
@@ -91,4 +91,5 @@
 	if( --num_startup > 0 ) return 0;
 
+        WSACancelAllAsyncRequests();
         winsockcleanupsockets();
 
@@ -167,6 +168,9 @@
   int i = set->fd_count;
 
-  while (i--)
-      if (set->fd_array[i] == s) return 1;
+  if(set) {
+  	while (i--) {
+      		if (set->fd_array[i] == s) return 1;
+	}
+  }
 
   return 0;
Index: trunk/src/wsock32/wsock2.cpp
===================================================================
--- trunk/src/wsock32/wsock2.cpp	(revision 7040)
+++ trunk/src/wsock32/wsock2.cpp	(revision 7041)
@@ -1,3 +1,3 @@
-/* $Id: wsock2.cpp,v 1.2 2001-07-07 14:29:40 achimha Exp $ */
+/* $Id: wsock2.cpp,v 1.3 2001-10-13 18:51:08 sandervl Exp $ */
 
 /*
@@ -36,5 +36,5 @@
 {
 // called too often in some apps, makes log file grow too fast
-//   dprintf(("WSAEnumNetworkEvents 0x%x 0x%x 0x%x NOT CORRECTLY IMPLEMENTED", s, hEvent, lpEvent));
+   dprintf2(("WSAEnumNetworkEvents 0x%x 0x%x 0x%x NOT CORRECTLY IMPLEMENTED", s, hEvent, lpEvent));
 
    if(!fWSAInitialized)
Index: trunk/src/wsock32/wsock32.cpp
===================================================================
--- trunk/src/wsock32/wsock32.cpp	(revision 7040)
+++ trunk/src/wsock32/wsock32.cpp	(revision 7041)
@@ -1,3 +1,3 @@
-/* $Id: wsock32.cpp,v 1.41 2001-10-11 19:24:07 sandervl Exp $ */
+/* $Id: wsock32.cpp,v 1.42 2001-10-13 18:51:08 sandervl Exp $ */
 
 /*
@@ -735,4 +735,6 @@
 {
    int ret;
+   int optlen;
+   int option;
 
    if(!fWSAInitialized) {
@@ -750,11 +752,11 @@
       	return SOCKET_ERROR;
    }
-//testestest
-#ifdef DEBUG
+   optlen = sizeof(option);
+   option = 0;
+   ret = getsockopt(s, IPPROTO_IP, IP_HDRINCL_OS2, (char *)&option, &optlen);
+   if(ret == 0 && option != FALSE) {
+       *(u_short *)&buf[2] = len;
+   }
    dprintf(("sending to %s", inet_ntoa(((sockaddr_in*)to)->sin_addr)));
-   for(int i=0;i<len;i++) {
-       dprintf(("%02x: %x", i, buf[i]));
-   }
-#endif
    ret = sendto(s, (char *)buf, len, flags, (struct sockaddr *)to, tolen);
 
@@ -764,5 +766,5 @@
    else WSASetLastError(NO_ERROR);
 
-   //Reset FD_WRITE event flagfor  WSAAsyncSelect thread if one was created for this socket
+   //Reset FD_WRITE event flag for WSAAsyncSelect thread if one was created for this socket
    EnableAsyncEvent(s, FD_WRITE);
    return ret;
@@ -1315,4 +1317,7 @@
                }
                ret = getsockopt(s, IPPROTO_IP, IP_HDRINCL_OS2, (char *)optval, optlen);
+               if(ret == 0) {
+                   ret = (ret != FALSE) ? TRUE : FALSE;
+               }
                break;
 
