Index: trunk/src/emx/src/lib/socket/bsdselect.c
===================================================================
--- trunk/src/emx/src/lib/socket/bsdselect.c	(revision 1157)
+++ trunk/src/emx/src/lib/socket/bsdselect.c	(revision 1158)
@@ -32,5 +32,7 @@
 #include <sys/socket.h>
 #include <sys/fcntl.h>
+#include <sys/time.h>
 #include <emx/io.h>
+#include <emx/libclog.h>
 #include "socket.h"
 
@@ -61,15 +63,27 @@
  * @param   c       Number of file descriptors specified in the call.
  * @param   pFrom   The select input fd_set structure.
- */
-static inline int calcsize(int c, const struct fd_set *pFrom)
+ * @parm    pcFDs   Store the new number of file descriptors (socket handles) to examin.
+ */
+static inline int calcsize(int c, const struct fd_set *pFrom, int *pcFDs)
 {
     int cbRet;
+    int i;
 #ifdef TCPV40HDRS
-    cbRet = (c + 7) / 8;
+    /* here we need to figure out the max real socket handle */
+    int iMax = *pcFDs - 1;
+    for (iMax = 0, i = 0; i < c; i++)
+        if (FD_ISSET(i, pFrom))
+        {
+            PLIBCSOCKETFH   pFHSocket = __libsocket_FH(i);
+            if (pFHSocket && iMax < pFHSocket->iSocket)
+                iMax = pFHSocket->iSocket;
+        }
+    cbRet = (iMax + 8) / 8;             /* iMax is index not count, thus +8 not +7. */
+    *pcFDs = iMax + 1;
 #else
-    int i;
     for (cbRet = sizeof(u_short), i = 0; i < c; i++)
         if (FD_ISSET(i, pFrom))
             cbRet++;
+    *pcFDs = c;
 #endif
     if (cbRet < sizeof(struct my_fd_set))
@@ -134,4 +148,6 @@
 int bsdselect(int nfds, struct fd_set *readfds, struct fd_set *writefds, struct fd_set *exceptfds, struct timeval *tv)
 {
+    LIBCLOG_ENTER("nfds=%d readfds=%p writefds=%p exceptfds=%d tv=%p={tv_sec=%d, rv_usec=%d}\n",
+                  nfds, readfds, writefds, exceptfds, tv, tv ? tv->tv_sec : -1, tv ? tv->tv_usec : -1);
 #ifdef TCPV40HDRS
     struct fd_set      *pRead;
@@ -143,6 +159,25 @@
     struct v5_fd_set   *pExcept;
 #endif
-    int                 cb;
     int                 rc;
+    int                 cb = 0;
+    int                 cFDs = 0;
+
+    /*
+     * Calculate bitmapsize.
+     */
+    if (readfds)
+        cb = calcsize(nfds, readfds, &cFDs);
+    if (writefds)
+    {
+        int cbT = calcsize(nfds, writefds, &cFDs);
+        if (cbT > cb)
+            cb = cbT;
+    }
+    if (exceptfds)
+    {
+        int cbT = calcsize(nfds, exceptfds, &cFDs);
+        if (cbT > cb)
+            cb = cbT;
+    }
 
     /*
@@ -152,13 +187,10 @@
     if (readfds)
     {
-        cb = calcsize(nfds, readfds);
         pRead = alloca(cb);
         if (!pRead)
         {
             __libsocket_setErrno(ENOMEM);
-            return -1;
-        }
-        if (convert(nfds, cb, readfds, pRead))
-            return -1;
+            LIBCLOG_RETURN_INT(-1);
+        }
     }
 
@@ -166,13 +198,10 @@
     if (writefds)
     {
-        cb = calcsize(nfds, writefds);
         pWrite = alloca(cb);
         if (!pWrite)
         {
             __libsocket_setErrno(ENOMEM);
-            return -1;
-        }
-        if (convert(nfds, cb, writefds, pWrite))
-            return -1;
+            LIBCLOG_RETURN_INT(-1);
+        }
     }
 
@@ -180,13 +209,31 @@
     if (exceptfds)
     {
-        cb = calcsize(nfds, exceptfds);
         pExcept = alloca(cb);
         if (!pExcept)
         {
             __libsocket_setErrno(ENOMEM);
-            return -1;
-        }
+            LIBCLOG_RETURN_INT(-1);
+        }
+    }
+
+    /*
+     * Convert the bitmaps.
+     */
+    if (readfds)
+    {
+        if (convert(nfds, cb, readfds, pRead))
+            LIBCLOG_RETURN_INT(-1);
+    }
+
+    if (writefds)
+    {
+        if (convert(nfds, cb, writefds, pWrite))
+            LIBCLOG_RETURN_INT(-1);
+    }
+
+    if (exceptfds)
+    {
         if (convert(nfds, cb, exceptfds, pExcept))
-            return -1;
+            LIBCLOG_RETURN_INT(-1);
     }
 
@@ -194,9 +241,9 @@
      * Do the call.
      */
-    rc = __libsocket_bsdselect(nfds, pRead, pWrite, pExcept, tv);
+    rc = __libsocket_bsdselect(cFDs, pRead, pWrite, pExcept, tv);
     if (rc < 0)
     {
         __libsocket_setLibcErrno();
-        return rc;
+        LIBCLOG_RETURN_INT(rc);
     }
 
@@ -214,5 +261,5 @@
         if (exceptfds)
             memset(exceptfds, 0, cb);
-        return 0;
+        LIBCLOG_RETURN_INT(0);
     }
 
@@ -227,7 +274,7 @@
         update(nfds, pExcept, exceptfds);
 
-    return rc;
-}
-
-
-
+    LIBCLOG_RETURN_INT(rc);
+}
+
+
+
Index: trunk/src/emx/src/lib/socket/socket.smak
===================================================================
--- trunk/src/emx/src/lib/socket/socket.smak	(revision 1157)
+++ trunk/src/emx/src/lib/socket/socket.smak	(revision 1158)
@@ -19,4 +19,9 @@
 include mkimplib.smak
 
+.TARGET	:= libsocket_l.a
+.TKIND	:= aout log
+.TKEEP	:= 1
+include mkimplib.smak
+
 .TARGET	:= libsocket.a
 .TKIND	:= aout
@@ -24,4 +29,5 @@
 include mkimplib.smak
 
+		
 	
 .TARGET	:= libsocket_p.a
@@ -36,4 +42,9 @@
 include mkimplib.smak
 
+.TARGET	:= libsocket_l.a
+.TKIND	:= aout log
+.TKEEP	:= 1
+include mkimplib.smak
+
 .TARGET	:= libsocket.a
 .TKIND	:= aout
Index: trunk/src/emx/src/libsocket/bsdselect.c
===================================================================
--- trunk/src/emx/src/libsocket/bsdselect.c	(revision 1157)
+++ trunk/src/emx/src/libsocket/bsdselect.c	(revision 1158)
@@ -32,5 +32,7 @@
 #include <sys/socket.h>
 #include <sys/fcntl.h>
+#include <sys/time.h>
 #include <emx/io.h>
+#include <emx/libclog.h>
 #include "socket.h"
 
@@ -61,15 +63,27 @@
  * @param   c       Number of file descriptors specified in the call.
  * @param   pFrom   The select input fd_set structure.
- */
-static inline int calcsize(int c, const struct fd_set *pFrom)
+ * @parm    pcFDs   Store the new number of file descriptors (socket handles) to examin.
+ */
+static inline int calcsize(int c, const struct fd_set *pFrom, int *pcFDs)
 {
     int cbRet;
+    int i;
 #ifdef TCPV40HDRS
-    cbRet = (c + 7) / 8;
+    /* here we need to figure out the max real socket handle */
+    int iMax = *pcFDs - 1;
+    for (iMax = 0, i = 0; i < c; i++)
+        if (FD_ISSET(i, pFrom))
+        {
+            PLIBCSOCKETFH   pFHSocket = __libsocket_FH(i);
+            if (pFHSocket && iMax < pFHSocket->iSocket)
+                iMax = pFHSocket->iSocket;
+        }
+    cbRet = (iMax + 8) / 8;             /* iMax is index not count, thus +8 not +7. */
+    *pcFDs = iMax + 1;
 #else
-    int i;
     for (cbRet = sizeof(u_short), i = 0; i < c; i++)
         if (FD_ISSET(i, pFrom))
             cbRet++;
+    *pcFDs = c;
 #endif
     if (cbRet < sizeof(struct my_fd_set))
@@ -134,4 +148,6 @@
 int bsdselect(int nfds, struct fd_set *readfds, struct fd_set *writefds, struct fd_set *exceptfds, struct timeval *tv)
 {
+    LIBCLOG_ENTER("nfds=%d readfds=%p writefds=%p exceptfds=%d tv=%p={tv_sec=%d, rv_usec=%d}\n",
+                  nfds, readfds, writefds, exceptfds, tv, tv ? tv->tv_sec : -1, tv ? tv->tv_usec : -1);
 #ifdef TCPV40HDRS
     struct fd_set      *pRead;
@@ -143,6 +159,25 @@
     struct v5_fd_set   *pExcept;
 #endif
-    int                 cb;
     int                 rc;
+    int                 cb = 0;
+    int                 cFDs = 0;
+
+    /*
+     * Calculate bitmapsize.
+     */
+    if (readfds)
+        cb = calcsize(nfds, readfds, &cFDs);
+    if (writefds)
+    {
+        int cbT = calcsize(nfds, writefds, &cFDs);
+        if (cbT > cb)
+            cb = cbT;
+    }
+    if (exceptfds)
+    {
+        int cbT = calcsize(nfds, exceptfds, &cFDs);
+        if (cbT > cb)
+            cb = cbT;
+    }
 
     /*
@@ -152,13 +187,10 @@
     if (readfds)
     {
-        cb = calcsize(nfds, readfds);
         pRead = alloca(cb);
         if (!pRead)
         {
             __libsocket_setErrno(ENOMEM);
-            return -1;
-        }
-        if (convert(nfds, cb, readfds, pRead))
-            return -1;
+            LIBCLOG_RETURN_INT(-1);
+        }
     }
 
@@ -166,13 +198,10 @@
     if (writefds)
     {
-        cb = calcsize(nfds, writefds);
         pWrite = alloca(cb);
         if (!pWrite)
         {
             __libsocket_setErrno(ENOMEM);
-            return -1;
-        }
-        if (convert(nfds, cb, writefds, pWrite))
-            return -1;
+            LIBCLOG_RETURN_INT(-1);
+        }
     }
 
@@ -180,13 +209,31 @@
     if (exceptfds)
     {
-        cb = calcsize(nfds, exceptfds);
         pExcept = alloca(cb);
         if (!pExcept)
         {
             __libsocket_setErrno(ENOMEM);
-            return -1;
-        }
+            LIBCLOG_RETURN_INT(-1);
+        }
+    }
+
+    /*
+     * Convert the bitmaps.
+     */
+    if (readfds)
+    {
+        if (convert(nfds, cb, readfds, pRead))
+            LIBCLOG_RETURN_INT(-1);
+    }
+
+    if (writefds)
+    {
+        if (convert(nfds, cb, writefds, pWrite))
+            LIBCLOG_RETURN_INT(-1);
+    }
+
+    if (exceptfds)
+    {
         if (convert(nfds, cb, exceptfds, pExcept))
-            return -1;
+            LIBCLOG_RETURN_INT(-1);
     }
 
@@ -194,9 +241,9 @@
      * Do the call.
      */
-    rc = __libsocket_bsdselect(nfds, pRead, pWrite, pExcept, tv);
+    rc = __libsocket_bsdselect(cFDs, pRead, pWrite, pExcept, tv);
     if (rc < 0)
     {
         __libsocket_setLibcErrno();
-        return rc;
+        LIBCLOG_RETURN_INT(rc);
     }
 
@@ -214,5 +261,5 @@
         if (exceptfds)
             memset(exceptfds, 0, cb);
-        return 0;
+        LIBCLOG_RETURN_INT(0);
     }
 
@@ -227,7 +274,7 @@
         update(nfds, pExcept, exceptfds);
 
-    return rc;
-}
-
-
-
+    LIBCLOG_RETURN_INT(rc);
+}
+
+
+
Index: trunk/src/emx/src/libsocket/socket.smak
===================================================================
--- trunk/src/emx/src/libsocket/socket.smak	(revision 1157)
+++ trunk/src/emx/src/libsocket/socket.smak	(revision 1158)
@@ -19,4 +19,9 @@
 include mkimplib.smak
 
+.TARGET	:= libsocket_l.a
+.TKIND	:= aout log
+.TKEEP	:= 1
+include mkimplib.smak
+
 .TARGET	:= libsocket.a
 .TKIND	:= aout
@@ -24,4 +29,5 @@
 include mkimplib.smak
 
+		
 	
 .TARGET	:= libsocket_p.a
@@ -36,4 +42,9 @@
 include mkimplib.smak
 
+.TARGET	:= libsocket_l.a
+.TKIND	:= aout log
+.TKEEP	:= 1
+include mkimplib.smak
+
 .TARGET	:= libsocket.a
 .TKIND	:= aout
