Index: trunk/src/emx/src/lib/socket/_getsockhandle.c
===================================================================
--- trunk/src/emx/src/lib/socket/_getsockhandle.c	(revision 886)
+++ trunk/src/emx/src/lib/socket/_getsockhandle.c	(revision 886)
@@ -0,0 +1,51 @@
+/* $Id$ */
+/** @file
+ *
+ * _getsockhandle()
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <emx/io.h>
+#include "socket.h"
+
+
+/**
+ * Gets the OS/2 socket handle.
+ *
+ * @returns OS/2 socket handle.
+ * @returns -1 on failure and errno set to EBADF or BNOSYS.
+ * @param   socket  LIBC socket handle.
+ */
+int _getsockhandle(int socket)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+        return pFHSocket->iSocket;
+    return -1;
+}
Index: trunk/src/emx/src/lib/socket/_impsockhandle.c
===================================================================
--- trunk/src/emx/src/lib/socket/_impsockhandle.c	(revision 886)
+++ trunk/src/emx/src/lib/socket/_impsockhandle.c	(revision 886)
@@ -0,0 +1,74 @@
+/* $Id$ */
+/** @file
+ *
+ * _getsockhandle()
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+
+/**
+ * Imports an socket handle from the OS/2 handle space.
+ *
+ * @returns LIBC socket handle.
+ * @returns -1 on failure and errno set to something sensible.
+ * @param   os2socket   OS/2 socket handle.
+ * @param   flags       Reserved for the future by EM - ignored.
+ */
+int _impsockhandle(int os2socket, int flags)
+{
+    PLIBCSOCKETFH   pFH;
+    int             iType;
+    int             cb;
+    int             fh;
+    int             rc;
+
+    /*
+     * Verify the socket.
+     */
+    cb = sizeof(iType);
+    if (__libsocket_getsockopt(os2socket, SOL_SOCKET, SO_TYPE, (char*)&iType, &cb) < 0)
+    {
+        __libsocket_setErrno(EBADF);
+        return -1;
+    }
+
+    /*
+     * Allocate LIBC handle.
+     */
+    rc = __libc_FHAllocate(-1, O_RDWR | F_SOCKET, sizeof(LIBCSOCKETFH), &__libsocket_gSocketOps, (PLIBCFH*)&pFH, &fh);
+    if (rc)
+        return -1;
+    pFH->iSocket = os2socket;
+    return fh;
+}
+
Index: trunk/src/emx/src/lib/socket/accept.c
===================================================================
--- trunk/src/emx/src/lib/socket/accept.c	(revision 885)
+++ trunk/src/emx/src/lib/socket/accept.c	(revision 886)
@@ -1,23 +1,65 @@
-/* accept.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
+/* $Id: $ */
+/** @file
+ *
+ * accept().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
 #include <sys/fcntl.h>
-#include <errno.h>
 #include <emx/io.h>
-#include <emx/syscalls.h>
+#include "socket.h"
 
-int accept (int handle, struct sockaddr *addr, int *paddrlen)
+int accept(int socket, struct sockaddr *addr, int *addrlen)
 {
-  int s, *pflags;
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    int             s;
+    if (pFHSocket)
+    {
+        s = __libsocket_accept(pFHSocket->iSocket, addr, addrlen);
+        if (s >= 0)
+        {
+            int             rc;
+            int             fh;
+            PLIBCSOCKETFH   pFH;
+            rc = __libc_FHAllocate(-1, O_RDWR | F_SOCKET, sizeof(LIBCSOCKETFH), &__libsocket_gSocketOps, (PLIBCFH*)&pFH, &fh);
+            if (!rc)
+            {
+                pFH->iSocket = s;
+                return fh;
+            }
+            else
+                __libsocket_soclose(s);
+            __libsocket_setSocketErrno();
+        }
+        else
+            __libsocket_setLibcErrno();
+    }
 
-  s = __accept (handle, addr, paddrlen);
-  if (s < 0)
     return -1;
-  if ((pflags = _fd_init (s)) == NULL)
-    {
-      __close (s);
-      errno = EMFILE;
-      return -1;
-    }
-  *pflags = O_RDWR | F_SOCKET;
-  return s;
 }
+
Index: trunk/src/emx/src/lib/socket/aliases/.readme
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/.readme	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/.readme	(revision 886)
@@ -0,0 +1,3 @@
+This directory contains __cdecl to _System aliases for libsocket functions
+which we wrap or implement. The libsocket functions which are imported
+directly have these aliases in the .imp files.
Index: trunk/src/emx/src/lib/socket/aliases/accept-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/accept-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/accept-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_accept",11,0,0,0;.stabs "accept",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/bind-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/bind-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/bind-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_bind",11,0,0,0;.stabs "bind",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/bsdselect-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/bsdselect-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/bsdselect-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_bsdselect",11,0,0,0;.stabs "bsdselect",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/connect-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/connect-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/connect-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_connect",11,0,0,0;.stabs "connect",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/getpeername-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/getpeername-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/getpeername-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_getpeername",11,0,0,0;.stabs "getpeername",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/getsockname-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/getsockname-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/getsockname-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_getsockname",11,0,0,0;.stabs "getsockname",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/getsockopt-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/getsockopt-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/getsockopt-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_getsockopt",11,0,0,0;.stabs "getsockopt",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/herror-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/herror-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/herror-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_herror",11,0,0,0;.stabs "herror",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/inetaddr-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/inetaddr-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/inetaddr-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_inetaddr",11,0,0,0;.stabs "inetaddr",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/inetnetw-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/inetnetw-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/inetnetw-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_inetnetw",11,0,0,0;.stabs "inetnetw",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/listen-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/listen-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/listen-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_listen",11,0,0,0;.stabs "listen",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/recv-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/recv-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/recv-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_recv",11,0,0,0;.stabs "recv",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/recvfrom-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/recvfrom-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/recvfrom-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_recvfrom",11,0,0,0;.stabs "recvfrom",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/recvmsg-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/recvmsg-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/recvmsg-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_recvmsg",11,0,0,0;.stabs "recvmsg",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/send-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/send-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/send-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_send",11,0,0,0;.stabs "send",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/sendmsg-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/sendmsg-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/sendmsg-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_sendmsg",11,0,0,0;.stabs "sendmsg",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/sendto-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/sendto-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/sendto-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_sendto",11,0,0,0;.stabs "sendto",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/setsockopt-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/setsockopt-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/setsockopt-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_setsockopt",11,0,0,0;.stabs "setsockopt",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/shutdown-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/shutdown-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/shutdown-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_shutdown",11,0,0,0;.stabs "shutdown",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/so_cancel-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/so_cancel-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/so_cancel-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_so_cancel",11,0,0,0;.stabs "so_cancel",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/so_ioctl-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/so_ioctl-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/so_ioctl-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_so_ioctl",11,0,0,0;.stabs "so_ioctl",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/so_readv-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/so_readv-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/so_readv-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_so_readv",11,0,0,0;.stabs "so_readv",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/so_writev-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/so_writev-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/so_writev-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_so_writev",11,0,0,0;.stabs "so_writev",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/soabort-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/soabort-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/soabort-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_soabort",11,0,0,0;.stabs "soabort",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/sock_errno-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/sock_errno-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/sock_errno-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_sock_errno",11,0,0,0;.stabs "sock_errno",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/socket-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/socket-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/socket-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_socket",11,0,0,0;.stabs "socket",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/socketops-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/socketops-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/socketops-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_socketops",11,0,0,0;.stabs "socketops",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/socketpair-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/socketpair-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/socketpair-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_socketpair",11,0,0,0;.stabs "socketpair",1,0,0,0
Index: trunk/src/emx/src/lib/socket/aliases/soclose-alias.s
===================================================================
--- trunk/src/emx/src/lib/socket/aliases/soclose-alias.s	(revision 886)
+++ trunk/src/emx/src/lib/socket/aliases/soclose-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_soclose",11,0,0,0;.stabs "soclose",1,0,0,0
Index: trunk/src/emx/src/lib/socket/bind.c
===================================================================
--- trunk/src/emx/src/lib/socket/bind.c	(revision 885)
+++ trunk/src/emx/src/lib/socket/bind.c	(revision 886)
@@ -1,8 +1,51 @@
-/* bind.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
+/* $Id: $ */
+/** @file
+ *
+ * bind().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
-#include <emx/syscalls.h>
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
 
-int bind (int handle, struct sockaddr *addr, int addrlen)
+int bind(int socket, const struct sockaddr *addr, int addrlen)
 {
-  return __bind (handle, addr, addrlen);
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_bind(pFHSocket->iSocket, addr, addrlen);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
 }
+
Index: trunk/src/emx/src/lib/socket/bsdselect.c
===================================================================
--- trunk/src/emx/src/lib/socket/bsdselect.c	(revision 885)
+++ trunk/src/emx/src/lib/socket/bsdselect.c	(revision 886)
@@ -1,82 +1,229 @@
-/* $Id$
- *
- * Wrapper for bsdselect() in BSD 4.4 mode.
- *
- * Copyright (c) 2003 InnoTek Systemberatung GmbH
- * Author: knut st. osmundsen <bird@anduin.net>
- *
- * All Rights Reserved
- *
- */
-#include <stdlib.h>
-#include <alloca.h>
-#include <sys/types.h>
-#include <sys/select.h>
-
-
-#define V5_FD_SETSIZE      64
-
-#pragma pack(4)
-typedef struct v5_fd_set {
-        u_short fd_count;               /* how many are SET? */
-        int     fd_array[V5_FD_SETSIZE];/* an array of SOCKETs */
-} v5_fd_set;
-#pragma pack()
-
-#define V5_FD_SET(fd, set) do { \
-    if (((v5_fd_set *)(set))->fd_count < V5_FD_SETSIZE) \
-        ((v5_fd_set *)(set))->fd_array[((v5_fd_set *)(set))->fd_count++]=fd;\
-} while(0)
-
-int _System v5_bsdselect(int, struct v5_fd_set *, struct v5_fd_set *, struct v5_fd_set *, struct timeval *);
-
-
-static inline void convert(int c, const struct fd_set *pFrom, struct v5_fd_set *pTo)
-{
-    int i = 0;
-
-    memset(pTo, 0, sizeof(struct v5_fd_set));
+/* $Id$ */
+/** @file
+ *
+ * bsdselect().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+
+/*******************************************************************************
+*   Defined Constants And Macros                                               *
+*******************************************************************************/
+#ifdef TCPV40HDRS
+
+#define MY_FD_SET(fd, set)      FD_SET(fd, set)
+#define MY_FD_ISSET(fd, set)    FD_ISSET(fd, set)
+#define MY_FD_ZERO(set, cb)     memset(set, 0, cb);
+#define my_fd_set               fd_set
+
+#else
+
+#define MY_FD_SET(fd, set)      V5_FD_SET(fd, set)
+#define	MY_FD_ISSET(fd, set)    V5_FD_ISSET(fd, set)
+#define MY_FD_ZERO(set, cb)     memset(set, 0, cb);
+#define my_fd_set               v5_fd_set
+
+#endif
+
+
+/**
+ * Calculate the size required for the converted set structure.
+ * @returns number of bytes.
+ * @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)
+{
+    int cbRet;
+#ifdef TCPV40HDRS
+    cbRet = (c + 7) / 8;
+#else
+    int i;
+    for (cbRet = sizeof(u_short), i = 0; i < c; i++)
+        if (FD_ISSET(i, pFrom))
+            cbRet++;
+#endif
+    if (cbRet < sizeof(struct my_fd_set))
+        return sizeof(struct my_fd_set);
+    return cbRet;
+}
+
+/** Converts the LIBC fd_set strcture pointed to by pFrom with it's LIBC socket handles,
+ * to the fd_set strcuture used by the OS/2 tcpip and the OS/2 socket handles.
+ * @returns 0 on success.
+ * @returns -1 on failure, both errnos set.
+ * @param   c       Number of file descriptors specified in the call.
+ * @param   cb      Size of pTo. (used for zeroing it)
+ * @param   pFrom   The select input fd_set structure.
+ * @param   pTo     The structure we present to OS/2 TCPIP.
+ *                  This will be initialized.
+ */
+static inline int convert(int c, int cb, const struct fd_set *pFrom, struct my_fd_set *pTo)
+{
+    int i;
+    MY_FD_ZERO(pTo, cb)
     for (i = 0; i < c; i++)
     {
         if (FD_ISSET(i, pFrom))
-            V5_FD_SET(i, pTo);
-    }
-    /** @todo: check for overflow and return appropriate error code... */
-}
-
+        {
+            PLIBCSOCKETFH   pFHSocket = __libsocket_FH(i);
+            if (!pFHSocket)
+                return -1;
+            MY_FD_SET(pFHSocket->iSocket, pTo);
+        }
+    }
+    return 0;
+}
 
 /**
- * This wrapper is required to work around a new ushort field someone
- * at IBM added to speed up the macros accessing the select structure.
- * @remark Only BSD 4.4 mode.
- */
-int _System bsdselect(int nfds, struct fd_set *readfds, struct fd_set *writefds,
-                      struct fd_set *excptfds, struct timeval *timeout)
-{
-    int     rc;
-    struct v5_fd_set *pv5_readfds  = NULL;
-    struct v5_fd_set *pv5_writefds = NULL;
-    struct v5_fd_set *pv5_excptfds = NULL;
-    int     cfds = nfds <= 0 ? FD_SETSIZE : nfds;
-
+ * Updates the pTo structure with the fds marked ready in pFrom.
+ *
+ * @param   c       Number of file descriptors specified in the call.
+ * @param   pFrom   The structure returned from OS/2 TCPIP select.
+ * @param   pTo     The structure passed in to select which have to
+ *                  be updated for the return.
+ */
+static inline void update(int c, const struct my_fd_set *pFrom, struct fd_set *pTo)
+{
+    int i;
+    for (i = 0; i < c; i++)
+    {
+        if (FD_ISSET(i, pTo))
+        {
+            PLIBCSOCKETFH   pFHSocket = __libsocket_FH(i);
+            if (pFHSocket)
+            {
+                if (!MY_FD_ISSET(pFHSocket->iSocket, pFrom))
+                    FD_CLR(i, pTo);
+            }
+        }
+    }
+}
+
+
+
+int bsdselect(int nfds, struct fd_set *readfds, struct fd_set *writefds, struct fd_set *exceptfds, struct timeval *tv)
+{
+#ifdef TCPV40HDRS
+    struct fd_set      *pRead;
+    struct fd_set      *pWrite;
+    struct fd_set      *pExcept;
+#else
+    struct v5_fd_set   *pRead;
+    struct v5_fd_set   *pWrite;
+    struct v5_fd_set   *pExcept;
+#endif
+    int                 cb;
+    int                 rc;
+
+    /*
+     * Allocate new bitmaps.
+     */
+    pRead = NULL;
     if (readfds)
     {
-        pv5_readfds = alloca(sizeof(struct v5_fd_set));
-        convert(cfds, readfds, pv5_readfds);
-    }
-
+        cb = calcsize(nfds, readfds);
+        pRead = alloca(cb);
+        if (!pRead)
+        {
+            __libsocket_setErrno(ENOMEM);
+            return -1;
+        }
+        if (convert(nfds, cb, readfds, pRead))
+            return -1;
+    }
+
+    pWrite = NULL;
     if (writefds)
     {
-        pv5_writefds = alloca(sizeof(struct v5_fd_set));
-        convert(cfds, writefds, pv5_writefds);
-    }
-
-    if (excptfds)
-    {
-        pv5_excptfds = alloca(sizeof(struct v5_fd_set));
-        convert(cfds, excptfds, pv5_excptfds);
-    }
-
-    rc = v5_bsdselect(nfds, pv5_readfds, pv5_writefds, pv5_excptfds, timeout);
+        cb = calcsize(nfds, writefds);
+        pWrite = alloca(cb);
+        if (!pWrite)
+        {
+            __libsocket_setErrno(ENOMEM);
+            return -1;
+        }
+        if (convert(nfds, cb, writefds, pWrite))
+            return -1;
+    }
+
+    pExcept = NULL;
+    if (exceptfds)
+    {
+        cb = calcsize(nfds, exceptfds);
+        pExcept = alloca(cb);
+        if (!pExcept)
+        {
+            __libsocket_setErrno(ENOMEM);
+            return -1;
+        }
+        if (convert(nfds, cb, exceptfds, pExcept))
+            return -1;
+    }
+
+    /*
+     * Do the call.
+     */
+    rc = __libsocket_bsdselect(nfds, pRead, pWrite, pExcept, tv);
+    if (rc < 0)
+    {
+        __libsocket_setLibcErrno();
+        return rc;
+    }
+
+    /*
+     * Timeout?
+     *  Just clear the bitmaps and return.
+     */
+    if (rc == 0)
+    {
+        cb = (nfds + 7) / 8;
+        if (readfds)
+            memset(readfds, 0, cb);
+        if (writefds)
+            memset(writefds, 0, cb);
+        if (exceptfds)
+            memset(exceptfds, 0, cb);
+        return 0;
+    }
+
+    /*
+     * Convert the bitmaps.
+     */
+    if (readfds)
+        update(nfds, pRead, readfds);
+    if (writefds)
+        update(nfds, pWrite, writefds);
+    if (exceptfds)
+        update(nfds, pExcept, exceptfds);
 
     return rc;
@@ -84,2 +231,3 @@
 
 
+
Index: trunk/src/emx/src/lib/socket/connect.c
===================================================================
--- trunk/src/emx/src/lib/socket/connect.c	(revision 885)
+++ trunk/src/emx/src/lib/socket/connect.c	(revision 886)
@@ -1,8 +1,51 @@
-/* connect.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
+/* $Id: $ */
+/** @file
+ *
+ * connect().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
-#include <emx/syscalls.h>
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
 
-int connect (int handle, struct sockaddr *addr, int addrlen)
+int connect(int socket, const struct sockaddr *addr, int addrlen)
 {
-  return __connect (handle, addr, addrlen);
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_connect(pFHSocket->iSocket, addr, addrlen);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
 }
+
Index: trunk/src/emx/src/lib/socket/gethosti.c
===================================================================
--- trunk/src/emx/src/lib/socket/gethosti.c	(revision 885)
+++ 	(revision )
@@ -1,12 +1,0 @@
-/* gethosti.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
-
-#include <emx/syscalls.h>
-
-int gethostid (void)
-{
-  int result;
-
-  if (__gethostid (&result) != 0)
-    result = -1;
-  return result;
-}
Index: trunk/src/emx/src/lib/socket/gethostn.c
===================================================================
--- trunk/src/emx/src/lib/socket/gethostn.c	(revision 885)
+++ 	(revision )
@@ -1,8 +1,0 @@
-/* gethostn.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
-
-#include <emx/syscalls.h>
-
-int gethostname (char *name, int len)
-{
-  return __gethostname (name, len);
-}
Index: trunk/src/emx/src/lib/socket/getpeern.c
===================================================================
--- trunk/src/emx/src/lib/socket/getpeern.c	(revision 885)
+++ 	(revision )
@@ -1,8 +1,0 @@
-/* getpeern.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
-
-#include <emx/syscalls.h>
-
-int getpeername (int handle, struct sockaddr *name, int *pnamelen)
-{
-  return __getpeername (handle, name, pnamelen);
-}
Index: trunk/src/emx/src/lib/socket/getpeername.c
===================================================================
--- trunk/src/emx/src/lib/socket/getpeername.c	(revision 886)
+++ trunk/src/emx/src/lib/socket/getpeername.c	(revision 886)
@@ -0,0 +1,51 @@
+/* $Id$ */
+/** @file
+ *
+ * getpeername().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+int getpeername(int socket, struct sockaddr *addr, int *addrlen)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_getpeername(pFHSocket->iSocket, addr, addrlen);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
+}
+
Index: trunk/src/emx/src/lib/socket/getsockh.c
===================================================================
--- trunk/src/emx/src/lib/socket/getsockh.c	(revision 885)
+++ 	(revision )
@@ -1,8 +1,0 @@
-/* getsockh.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
-
-#include <emx/syscalls.h>
-
-int _getsockhandle (int handle)
-{
-  return __getsockhandle (handle);
-}
Index: trunk/src/emx/src/lib/socket/getsockn.c
===================================================================
--- trunk/src/emx/src/lib/socket/getsockn.c	(revision 885)
+++ 	(revision )
@@ -1,8 +1,0 @@
-/* getsockn.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
-
-#include <emx/syscalls.h>
-
-int getsockname (int handle, struct sockaddr *name, int *pnamelen)
-{
-  return __getsockname (handle, name, pnamelen);
-}
Index: trunk/src/emx/src/lib/socket/getsockname.c
===================================================================
--- trunk/src/emx/src/lib/socket/getsockname.c	(revision 886)
+++ trunk/src/emx/src/lib/socket/getsockname.c	(revision 886)
@@ -0,0 +1,52 @@
+/* $Id$ */
+/** @file
+ *
+ * getsockname().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+int getsockname(int socket, struct sockaddr *addr, int *addrlen)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_getsockname(pFHSocket->iSocket, addr, addrlen);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
+}
+
+
Index: trunk/src/emx/src/lib/socket/getsocko.c
===================================================================
--- trunk/src/emx/src/lib/socket/getsocko.c	(revision 885)
+++ 	(revision )
@@ -1,8 +1,0 @@
-/* getsocko.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
-
-#include <emx/syscalls.h>
-
-int getsockopt (int handle, int level, int optname, void *optval, int *poptlen)
-{
-  return __getsockopt (handle, level, optname, optval, poptlen);
-}
Index: trunk/src/emx/src/lib/socket/getsockopt.c
===================================================================
--- trunk/src/emx/src/lib/socket/getsockopt.c	(revision 886)
+++ trunk/src/emx/src/lib/socket/getsockopt.c	(revision 886)
@@ -0,0 +1,52 @@
+/* $Id$ */
+/** @file
+ *
+ * getsockopt().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+int getsockopt(int socket, int level, int optname, void *optval, int *optlen)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_getsockopt(pFHSocket->iSocket, level, optname, optval, optlen);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
+}
+
+
Index: trunk/src/emx/src/lib/socket/ghostbya.c
===================================================================
--- trunk/src/emx/src/lib/socket/ghostbya.c	(revision 885)
+++ 	(revision )
@@ -1,27 +1,0 @@
-/* ghostbya.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
-
-#include <stdlib.h>
-#include <sys/types.h>
-#include <netdb.h>
-#include <errno.h>
-#include <emx/syscalls.h>
-
-extern int h_errno;
-
-struct hostent *gethostbyaddr (const char *addr, int len, int type)
-{
-  struct hostent *result;
-  int saved_errno;
-
-  saved_errno = errno;
-  if (__gethostbyaddr (addr, len, type, &result) != 0)
-    {
-      if (errno > 4)            /* ENOSYS! */
-        h_errno = NO_RECOVERY;
-      else
-        h_errno = errno;
-      result = NULL;
-    }
-  errno = saved_errno;
-  return result;
-}
Index: trunk/src/emx/src/lib/socket/ghostbyn.c
===================================================================
--- trunk/src/emx/src/lib/socket/ghostbyn.c	(revision 885)
+++ 	(revision )
@@ -1,27 +1,0 @@
-/* ghostbyn.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
-
-#include <stdlib.h>
-#include <sys/types.h>
-#include <netdb.h>
-#include <errno.h>
-#include <emx/syscalls.h>
-
-extern int h_errno;
-
-struct hostent *gethostbyname (const char *name)
-{
-  struct hostent *result;
-  int saved_errno;
-
-  saved_errno = errno;
-  if (__gethostbyname (name, &result) != 0)
-    {
-      if (errno > 4)            /* ENOSYS! */
-        h_errno = NO_RECOVERY;
-      else
-        h_errno = errno;
-      result = NULL;
-    }
-  errno = saved_errno;
-  return result;
-}
Index: trunk/src/emx/src/lib/socket/gnetbyad.c
===================================================================
--- trunk/src/emx/src/lib/socket/gnetbyad.c	(revision 885)
+++ 	(revision )
@@ -1,13 +1,0 @@
-/* gnetbyad.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
-
-#include <stdlib.h>
-#include <emx/syscalls.h>
-
-struct netent *getnetbyaddr (long net)
-{
-  struct netent *result;
-
-  if (__getnetbyaddr (net, &result) != 0)
-    result = NULL;
-  return result;
-}
Index: trunk/src/emx/src/lib/socket/gnetbyna.c
===================================================================
--- trunk/src/emx/src/lib/socket/gnetbyna.c	(revision 885)
+++ 	(revision )
@@ -1,13 +1,0 @@
-/* gnetbyna.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
-
-#include <stdlib.h>
-#include <emx/syscalls.h>
-
-struct netent *getnetbyname (const char *name)
-{
-  struct netent *result;
-
-  if (__getnetbyname (name, &result) != 0)
-    result = NULL;
-  return result;
-}
Index: trunk/src/emx/src/lib/socket/gprotbna.c
===================================================================
--- trunk/src/emx/src/lib/socket/gprotbna.c	(revision 885)
+++ 	(revision )
@@ -1,13 +1,0 @@
-/* gprotbna.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
-
-#include <stdlib.h>
-#include <emx/syscalls.h>
-
-struct protoent *getprotobyname (const char *name)
-{
-  struct protoent *result;
-
-  if (__getprotobyname (name, &result) != 0)
-    result = NULL;
-  return result;
-}
Index: trunk/src/emx/src/lib/socket/gprotbnu.c
===================================================================
--- trunk/src/emx/src/lib/socket/gprotbnu.c	(revision 885)
+++ 	(revision )
@@ -1,13 +1,0 @@
-/* gprotbnu.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
-
-#include <stdlib.h>
-#include <emx/syscalls.h>
-
-struct protoent *getprotobynumber (int proto)
-{
-  struct protoent *result;
-
-  if (__getprotobynumber (proto, &result) != 0)
-    result = NULL;
-  return result;
-}
Index: trunk/src/emx/src/lib/socket/gservbyn.c
===================================================================
--- trunk/src/emx/src/lib/socket/gservbyn.c	(revision 885)
+++ 	(revision )
@@ -1,13 +1,0 @@
-/* gservbyn.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
-
-#include <stdlib.h>
-#include <emx/syscalls.h>
-
-struct servent *getservbyname (const char *name, const char *proto)
-{
-  struct servent *result;
-
-  if (__getservbyname (name, proto, &result) != 0)
-    result = NULL;
-  return result;
-}
Index: trunk/src/emx/src/lib/socket/gservbyp.c
===================================================================
--- trunk/src/emx/src/lib/socket/gservbyp.c	(revision 885)
+++ 	(revision )
@@ -1,13 +1,0 @@
-/* gservbyp.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
-
-#include <stdlib.h>
-#include <emx/syscalls.h>
-
-struct servent *getservbyport (int port, const char *proto)
-{
-  struct servent *result;
-
-  if (__getservbyport (port, proto, &result) != 0)
-    result = NULL;
-  return result;
-}
Index: trunk/src/emx/src/lib/socket/herrno.c
===================================================================
--- trunk/src/emx/src/lib/socket/herrno.c	(revision 885)
+++ 	(revision )
@@ -1,6 +1,0 @@
-/* herrno.c (emx+gcc) -- Copyright (c) 1994 by Eberhard Mattes */
-
-/* This variable must be initialized as no extended dictionary entries
-   are built for COMDEF. */
-
-int h_errno = 0;
Index: trunk/src/emx/src/lib/socket/herror.c
===================================================================
--- trunk/src/emx/src/lib/socket/herror.c	(revision 885)
+++ trunk/src/emx/src/lib/socket/herror.c	(revision 886)
@@ -1,5 +1,9 @@
 /* herror.c (emx+gcc) -- Copyright (c) 1994 by Eberhard Mattes */
 
-#define TCPV40HDRS
+#ifndef TCPV40HDRS
+#error TCPV40HDRS only
+#endif
+
+#include "libc-alias.h"
 #include <stdio.h>
 #include <sys/types.h>
@@ -8,5 +12,5 @@
 extern int h_errno;
 
-void herror (const char *string)
+void TCPCALL herror(const char *string)
 {
   int e = h_errno;
Index: trunk/src/emx/src/lib/socket/impsockh.c
===================================================================
--- trunk/src/emx/src/lib/socket/impsockh.c	(revision 885)
+++ 	(revision )
@@ -1,23 +1,0 @@
-/* impsockh.c (emx+gcc) -- Copyright (c) 1995-1996 by Eberhard Mattes */
-
-#include <fcntl.h>
-#include <errno.h>
-#include <emx/io.h>
-#include <emx/syscalls.h>
-
-int _impsockhandle (int handle, int flags)
-{
-  int fd, *pflags;
-
-  fd = __impsockhandle (handle, flags);
-  if (fd == -1)
-    return -1;
-  if ((pflags = _fd_init (fd)) == NULL)
-    {
-      errno = EMFILE;
-      return -1;
-    }
-
-  *pflags = O_RDWR | F_SOCKET;
-  return fd;
-}
Index: trunk/src/emx/src/lib/socket/inetaddr.c
===================================================================
--- trunk/src/emx/src/lib/socket/inetaddr.c	(revision 885)
+++ 	(revision )
@@ -1,52 +1,0 @@
-/* inetaddr.c (emx+gcc) -- Copyright (c) 1994-1995 by Eberhard Mattes */
-
-#include <stdlib.h>
-#include <ctype.h>
-#include <sys/types.h>
-#include <netinet/in.h>
-
-unsigned long inet_addr (const char *s)
-{
-  unsigned long numbers[4];
-  unsigned long r;
-  int i;
-  char *e;
-  long n;
-
-  if (!isdigit ((unsigned char)*s))
-    return INADDR_NONE;
-  for (i = 0; i < 4; ++i)
-    {
-      n = strtol (s, &e, 0);
-      if (e == s)
-        return INADDR_NONE;
-      s = e;
-      numbers[i] = n;
-      if (*s != '.')
-        break;
-      ++s;
-      if (!isdigit ((unsigned char)*s))
-        return INADDR_NONE;
-    }
-  if (*s != 0 && !isspace ((unsigned char)*s))
-    return INADDR_NONE;
-
-  switch (i)
-    {
-    case 0:
-      r = numbers[0];
-      break;
-    case 1:
-      r = (numbers[0] << 24) | (numbers[1] & 0xffffff);
-      break;
-    case 2:
-      r = ((numbers[0] << 24) | ((numbers[1] & 0xff) << 16)
-           | (numbers[2] & 0xffff));
-      break;
-    default:
-      r = ((numbers[0] << 24) | ((numbers[1] & 0xff) << 16)
-           | ((numbers[2] & 0xff) << 8) | (numbers[3] & 0xff));
-      break;
-    }
-  return htonl (r);
-}
Index: trunk/src/emx/src/lib/socket/inetlnao.c
===================================================================
--- trunk/src/emx/src/lib/socket/inetlnao.c	(revision 885)
+++ 	(revision )
@@ -1,17 +1,0 @@
-/* inetlnao.c (emx+gcc) -- Copyright (c) 1994-1995 by Eberhard Mattes */
-
-#include <sys/types.h>
-#include <netinet/in.h>
-
-unsigned long inet_lnaof (struct in_addr in)
-{
-  unsigned long temp;
-
-  temp = ntohl (in.s_addr);
-  if (IN_CLASSA (temp))
-    return temp & IN_CLASSA_HOST;
-  else if (IN_CLASSB (temp))
-    return temp & IN_CLASSB_HOST;
-  else
-    return temp & IN_CLASSC_HOST;
-}
Index: trunk/src/emx/src/lib/socket/inetmkad.c
===================================================================
--- trunk/src/emx/src/lib/socket/inetmkad.c	(revision 885)
+++ 	(revision )
@@ -1,20 +1,0 @@
-/* inetmkad.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
-
-#include <sys/types.h>
-#include <netinet/in.h>
-
-struct in_addr inet_makeaddr (u_long net, u_long host)
-{
-  unsigned long unet;
-  struct in_addr in;
-
-  unet = net;
-  if (unet < 0xc0)
-    in.s_addr = (unet << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
-  else if (unet < 0x10000)
-    in.s_addr = (unet << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST);
-  else
-    in.s_addr = (unet << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST);
-  in.s_addr = htonl (in.s_addr);
-  return in;
-}
Index: trunk/src/emx/src/lib/socket/inetneto.c
===================================================================
--- trunk/src/emx/src/lib/socket/inetneto.c	(revision 885)
+++ 	(revision )
@@ -1,17 +1,0 @@
-/* inetneto.c (emx+gcc) -- Copyright (c) 1994-1995 by Eberhard Mattes */
-
-#include <sys/types.h>
-#include <netinet/in.h>
-
-unsigned long inet_netof (struct in_addr in)
-{
-  unsigned long temp;
-
-  temp = ntohl (in.s_addr);
-  if (IN_CLASSA (temp))
-    return (temp & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT;
-  else if (IN_CLASSB (temp))
-    return (temp & IN_CLASSB_NET) >> IN_CLASSB_NSHIFT;
-  else
-    return (temp & IN_CLASSC_NET) >> IN_CLASSC_NSHIFT;
-}
Index: trunk/src/emx/src/lib/socket/inetnetw.c
===================================================================
--- trunk/src/emx/src/lib/socket/inetnetw.c	(revision 885)
+++ 	(revision )
@@ -1,52 +1,0 @@
-/* inetnetw.c (emx+gcc) -- Copyright (c) 1994-1995 by Eberhard Mattes */
-
-#include <stdlib.h>
-#include <ctype.h>
-#include <sys/types.h>
-#include <netinet/in.h>
-
-unsigned long inet_network (const char *s)
-{
-  unsigned long numbers[4];
-  unsigned long r;
-  int i;
-  char *e;
-  long n;
-
-  if (!isdigit ((unsigned char)*s))
-    return INADDR_NONE;
-  for (i = 0; i < 4; ++i)
-    {
-      n = strtol (s, &e, 0);
-      if (e == s)
-        return INADDR_NONE;
-      s = e;
-      numbers[i] = n;
-      if (*s != '.')
-        break;
-      ++s;
-      if (!isdigit ((unsigned char)*s))
-        return INADDR_NONE;
-    }
-  if (*s != 0 && !isspace ((unsigned char)*s))
-    return INADDR_NONE;
-
-  switch (i)
-    {
-    case 0:
-      r = numbers[0] & 0xff;
-      break;
-    case 1:
-      r = ((numbers[0] & 0xff) << 8) | (numbers[1] & 0xff);
-      break;
-    case 2:
-      r = (((numbers[0] & 0xff) << 16) | ((numbers[1] & 0xff) << 8)
-           | (numbers[2] & 0xff));
-      break;
-    default:
-      r = (((numbers[0] & 0xff) << 24) | ((numbers[1] & 0xff) << 16)
-           | ((numbers[2] & 0xff) << 8) | (numbers[3] & 0xff));
-      break;
-    }
-  return r;
-}
Index: trunk/src/emx/src/lib/socket/inetntoa.c
===================================================================
--- trunk/src/emx/src/lib/socket/inetntoa.c	(revision 885)
+++ 	(revision )
@@ -1,17 +1,0 @@
-/* inetntoa.c (emx+gcc) -- Copyright (c) 1994-1998 by Eberhard Mattes */
-
-#include <stdio.h>
-#include <sys/types.h>
-#include <netinet/in.h>
-#include <emx/thread.h>
-
-char *inet_ntoa (struct in_addr in)
-{
-  struct _thread *tp = _thread ();
-  unsigned char *t;
-
-  t = (unsigned char *)&in;
-  sprintf (tp->_th_inetntoa, "%d.%d.%d.%d",
-           t[0] & 0xff, t[1] & 0xff, t[2] & 0xff, t[3] & 0xff);
-  return tp->_th_inetntoa;
-}
Index: trunk/src/emx/src/lib/socket/listen.c
===================================================================
--- trunk/src/emx/src/lib/socket/listen.c	(revision 885)
+++ trunk/src/emx/src/lib/socket/listen.c	(revision 886)
@@ -1,8 +1,52 @@
-/* listen.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
+/* $Id: $ */
+/** @file
+ *
+ * listen().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
-#include <emx/syscalls.h>
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
 
-int listen (int handle, int backlog)
+int listen(int socket, int backlog)
 {
-  return __listen (handle, backlog);
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_listen(pFHSocket->iSocket, backlog);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
 }
+
+
Index: trunk/src/emx/src/lib/socket/os2_ioctl.c
===================================================================
--- trunk/src/emx/src/lib/socket/os2_ioctl.c	(revision 886)
+++ trunk/src/emx/src/lib/socket/os2_ioctl.c	(revision 886)
@@ -0,0 +1,61 @@
+/* $Id$ */
+/** @file
+ *
+ * ioctl().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <stdarg.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+#ifdef TCPV40HDRS
+int os2_ioctl(int socket, int request, char *arg, int len_arg)
+#else
+int os2_ioctl(int socket, unsigned long request, char *arg, int len_arg)
+#endif
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+#ifdef TCPV40HDRS
+        rc = __libsocket_os2_ioctl(pFHSocket->iSocket, __IOCLW(request), arg, len_arg);
+#else
+        rc = __libsocket_os2_ioctl(pFHSocket->iSocket, request, arg, len_arg);
+#endif
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+    return -1;
+}
+
+
+
Index: trunk/src/emx/src/lib/socket/os2_select.c
===================================================================
--- trunk/src/emx/src/lib/socket/os2_select.c	(revision 886)
+++ trunk/src/emx/src/lib/socket/os2_select.c	(revision 886)
@@ -0,0 +1,76 @@
+/* $Id$ */
+/** @file
+ *
+ * os2_select().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+int os2_select(int *s, int nreads, int nwrites, int nexcepts, long timeout)
+{
+    int     cSockets;
+    int    *aSockets;
+    int     i;
+    int     rc;
+
+    /*
+     * Convert the sockets.
+     */
+    cSockets = nreads + nwrites + nexcepts;
+    aSockets = alloca(cSockets * sizeof(int));
+    if (aSockets)
+    {
+        __libsocket_setErrno(ENOMEM);
+        return -1;
+    }
+    for (i = 0; i < cSockets; i++)
+    {
+        PLIBCSOCKETFH   pFHSocket = __libsocket_FH(s[i]);
+        if (!pFHSocket)
+        {
+            __libsocket_setErrno(EBADF);
+            return -1;
+        }
+        aSockets[i] = pFHSocket->iSocket;
+    }
+
+    /*
+     * Do the call with new array.
+     */
+    rc = __libsocket_os2_select(aSockets, nreads, nwrites, nexcepts, timeout);
+    if (rc >= 0)
+        return rc;
+    __libsocket_setLibcErrno();
+    return -1;
+}
+
+
+
Index: trunk/src/emx/src/lib/socket/recv.c
===================================================================
--- trunk/src/emx/src/lib/socket/recv.c	(revision 885)
+++ trunk/src/emx/src/lib/socket/recv.c	(revision 886)
@@ -1,8 +1,52 @@
-/* recv.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
+/* $Id: $ */
+/** @file
+ *
+ * recv().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
-#include <emx/syscalls.h>
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
 
-int recv (int handle, void *buf, int len, unsigned flags)
+int recv(int socket, void *buf, int len, int flags)
 {
-  return __recv (handle, buf, len, flags);
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_recv(pFHSocket->iSocket, buf, len, flags);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
 }
+
+
Index: trunk/src/emx/src/lib/socket/recvfrom.c
===================================================================
--- trunk/src/emx/src/lib/socket/recvfrom.c	(revision 885)
+++ trunk/src/emx/src/lib/socket/recvfrom.c	(revision 886)
@@ -1,17 +1,52 @@
-/* recvfrom.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
+/* $Id: $ */
+/** @file
+ *
+ * recvfrom().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
-#include <emx/syscalls.h>
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
 
-int recvfrom (int handle, void *buf, int len, unsigned flags,
-              struct sockaddr *from, int *pfromlen)
+int recvfrom(int socket, void *buf, int len, int flags, struct sockaddr *from, int *fromlen)
 {
-  struct _recvfrom args;
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_recvfrom(pFHSocket->iSocket, buf, len, flags, from, fromlen);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
 
-  args.handle   = handle;
-  args.buf      = buf;
-  args.len      = len;
-  args.flags    = flags;
-  args.from     = from;
-  args.pfromlen = pfromlen;
-  return __recvfrom (&args);
+    return -1;
 }
+
+
Index: trunk/src/emx/src/lib/socket/recvmsg.c
===================================================================
--- trunk/src/emx/src/lib/socket/recvmsg.c	(revision 886)
+++ trunk/src/emx/src/lib/socket/recvmsg.c	(revision 886)
@@ -0,0 +1,52 @@
+/* $Id$ */
+/** @file
+ *
+ * recvmsg().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+int recvmsg(int socket, struct msghdr *msg, int flags)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_recvmsg(pFHSocket->iSocket, msg, flags);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
+}
+
+
Index: trunk/src/emx/src/lib/socket/send.c
===================================================================
--- trunk/src/emx/src/lib/socket/send.c	(revision 885)
+++ trunk/src/emx/src/lib/socket/send.c	(revision 886)
@@ -1,8 +1,52 @@
-/* send.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
+/* $Id: $ */
+/** @file
+ *
+ * send().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
-#include <emx/syscalls.h>
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
 
-int send (int handle, const void *buf, int len, unsigned flags)
+int send(int socket, const void *buf, int len, int flags)
 {
-  return __send (handle, buf, len, flags);
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_send(pFHSocket->iSocket, buf, len, flags);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
 }
+
+
Index: trunk/src/emx/src/lib/socket/sendmsg.c
===================================================================
--- trunk/src/emx/src/lib/socket/sendmsg.c	(revision 886)
+++ trunk/src/emx/src/lib/socket/sendmsg.c	(revision 886)
@@ -0,0 +1,52 @@
+/* $Id$ */
+/** @file
+ *
+ * sendmsg().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+int sendmsg(int socket, const struct msghdr *msg, int flags)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_sendmsg(pFHSocket->iSocket, msg, flags);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
+}
+
+
Index: trunk/src/emx/src/lib/socket/sendto.c
===================================================================
--- trunk/src/emx/src/lib/socket/sendto.c	(revision 885)
+++ trunk/src/emx/src/lib/socket/sendto.c	(revision 886)
@@ -1,17 +1,51 @@
-/* sendto.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
+/* $Id: $ */
+/** @file
+ *
+ * sendto().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
-#include <emx/syscalls.h>
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
 
-int sendto (int handle, const void *buf, int len, unsigned flags,
-            const struct sockaddr *to, int tolen)
+int sendto(int socket, const void *buf, int len, int flags, const struct sockaddr *to, int tolen)
 {
-  struct _sendto args;
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int     rc;
+        rc = __libsocket_sendto(pFHSocket->iSocket, buf, len, flags, to, tolen);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
 
-  args.handle = handle;
-  args.buf    = buf;
-  args.len    = len;
-  args.flags  = flags;
-  args.to     = to;
-  args.tolen  = tolen;
-  return __sendto (&args);
+    return -1;
 }
+
Index: trunk/src/emx/src/lib/socket/setsocko.c
===================================================================
--- trunk/src/emx/src/lib/socket/setsocko.c	(revision 885)
+++ 	(revision )
@@ -1,9 +1,0 @@
-/* setsocko.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
-
-#include <emx/syscalls.h>
-
-int setsockopt (int handle, int level, int optname, const void *optval,
-                int optlen)
-{
-  return __setsockopt (handle, level, optname, optval, optlen);
-}
Index: trunk/src/emx/src/lib/socket/setsockopt.c
===================================================================
--- trunk/src/emx/src/lib/socket/setsockopt.c	(revision 886)
+++ trunk/src/emx/src/lib/socket/setsockopt.c	(revision 886)
@@ -0,0 +1,51 @@
+/* $Id$ */
+/** @file
+ *
+ * setsockopt().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+int setsockopt(int socket, int level, int optname, const void *optval, int optlen)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_setsockopt(pFHSocket->iSocket, level, optname, optval, optlen);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
+}
+
Index: trunk/src/emx/src/lib/socket/shutdown.c
===================================================================
--- trunk/src/emx/src/lib/socket/shutdown.c	(revision 885)
+++ trunk/src/emx/src/lib/socket/shutdown.c	(revision 886)
@@ -1,8 +1,52 @@
-/* shutdown.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
+/* $Id: $ */
+/** @file
+ *
+ * shutdown().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
-#include <emx/syscalls.h>
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
 
-int shutdown (int handle, int how)
+int shutdown(int socket, int how)
 {
-  return __shutdown (handle, how);
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_shutdown(pFHSocket->iSocket, how);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
 }
+
+
Index: trunk/src/emx/src/lib/socket/so_cancel.c
===================================================================
--- trunk/src/emx/src/lib/socket/so_cancel.c	(revision 886)
+++ trunk/src/emx/src/lib/socket/so_cancel.c	(revision 886)
@@ -0,0 +1,52 @@
+/* $Id$ */
+/** @file
+ *
+ * so_cancel().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+int so_cancel(int socket)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_so_cancel(pFHSocket->iSocket);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
+}
+
+
Index: trunk/src/emx/src/lib/socket/so_ioctl.c
===================================================================
--- trunk/src/emx/src/lib/socket/so_ioctl.c	(revision 886)
+++ trunk/src/emx/src/lib/socket/so_ioctl.c	(revision 886)
@@ -0,0 +1,54 @@
+/* $Id$ */
+/** @file
+ *
+ * ioctl().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <stdarg.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+int so_ioctl(int socket, unsigned long request, ...)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        va_list args;
+        int     iArg;
+        va_start(args, request);
+        iArg = va_arg(args, int);
+        va_end(args);
+        return ioctl(socket, request, iArg);
+    }
+    return -1;
+}
+
+
+
Index: trunk/src/emx/src/lib/socket/so_readv.c
===================================================================
--- trunk/src/emx/src/lib/socket/so_readv.c	(revision 886)
+++ trunk/src/emx/src/lib/socket/so_readv.c	(revision 886)
@@ -0,0 +1,52 @@
+/* $Id$ */
+/** @file
+ *
+ * so_readv().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+ssize_t so_readv(int socket, struct iovec *iov, int iovcnt)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        ssize_t rc;
+        rc = __libsocket_so_readv(pFHSocket->iSocket, iov, iovcnt);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
+}
+
+
Index: trunk/src/emx/src/lib/socket/so_writev.c
===================================================================
--- trunk/src/emx/src/lib/socket/so_writev.c	(revision 886)
+++ trunk/src/emx/src/lib/socket/so_writev.c	(revision 886)
@@ -0,0 +1,52 @@
+/* $Id$ */
+/** @file
+ *
+ * so_writev().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+ssize_t so_writev(int socket, struct iovec *iov, int iovcnt)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        ssize_t rc;
+        rc = __libsocket_so_writev(pFHSocket->iSocket, iov, iovcnt);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
+}
+
+
Index: trunk/src/emx/src/lib/socket/soabort.c
===================================================================
--- trunk/src/emx/src/lib/socket/soabort.c	(revision 886)
+++ trunk/src/emx/src/lib/socket/soabort.c	(revision 886)
@@ -0,0 +1,55 @@
+/* $Id$ */
+/** @file
+ *
+ * soabort().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+/* not declared anywhere */
+int	TCPCALL soabort(int fh);
+
+int soabort(int socket)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_soabort(pFHSocket->iSocket);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
+}
+
+
Index: trunk/src/emx/src/lib/socket/sock_errno.c
===================================================================
--- trunk/src/emx/src/lib/socket/sock_errno.c	(revision 885)
+++ trunk/src/emx/src/lib/socket/sock_errno.c	(revision 886)
@@ -10,6 +10,14 @@
  */
 
-#include <sys/types.h>
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
 #include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
 
 /**
@@ -24,6 +32,6 @@
     /* get the OS/2 error. */
     int err = os2_sock_errno();
-    if (err > 10000 && err < 11000)
-        err -= 10000;
+    if (err > EOS2_TCPIP_OFFSET && err < EOS2_TCPIP_OFFSET + 1000)
+        err -= EOS2_TCPIP_OFFSET;
     return err;
 }
Index: trunk/src/emx/src/lib/socket/socket.c
===================================================================
--- trunk/src/emx/src/lib/socket/socket.c	(revision 885)
+++ trunk/src/emx/src/lib/socket/socket.c	(revision 886)
@@ -1,23 +1,63 @@
-/* socket.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
+/* $Id: $ */
+/** @file
+ *
+ * socket().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
 #include <sys/fcntl.h>
-#include <errno.h>
 #include <emx/io.h>
-#include <emx/syscalls.h>
+#include "socket.h"
 
-int socket (int domain, int type, int protocol)
+
+int socket(int af, int type, int protocol)
 {
-  int s, *pflags;
+    int s;
 
-  s = __socket (domain, type, protocol);
-  if (s < 0)
+    s = __libsocket_socket(af, type, protocol);
+    if (s >= 0)
+    {
+        int             rc;
+        int             fh;
+        PLIBCSOCKETFH   pFH;
+        rc = __libc_FHAllocate(-1, O_RDWR | F_SOCKET, sizeof(LIBCSOCKETFH), &__libsocket_gSocketOps, (PLIBCFH*)&pFH, &fh);
+        if (!rc)
+        {
+            pFH->iSocket = s;
+            return fh;
+        }
+        else
+            __libsocket_soclose(s);
+        __libsocket_setSocketErrno();
+    }
+    else
+        __libsocket_setLibcErrno();
+
     return -1;
-  if ((pflags = _fd_init (s)) == NULL)
-    {
-      __close (s);
-      errno = EMFILE;
-      return -1;
-    }
-  *pflags = O_RDWR | F_SOCKET;
-  return s;
 }
+
Index: trunk/src/emx/src/lib/socket/socket.h
===================================================================
--- trunk/src/emx/src/lib/socket/socket.h	(revision 886)
+++ trunk/src/emx/src/lib/socket/socket.h	(revision 886)
@@ -0,0 +1,232 @@
+/* $Id$ */
+/** @file
+ *
+ * Interal libsocket stuff.
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#ifndef __socket_h__
+#define __socket_h__
+
+
+/** @group libsocket    libsocket
+ * @{
+ */
+
+/*******************************************************************************
+*   Defined Constants And Macros                                               *
+*******************************************************************************/
+/** The offset the OS/2 TCP/IP errno values are skewed compared to the
+ * LIBC errno values. */
+#define EOS2_TCPIP_OFFSET   10000
+
+/** Get the low word of the ioctl request number.
+ * Used to support ioctl request numbers from old and new _IOC macros.
+ */
+#define __IOCLW(a) ((unsigned short)(a))
+
+#ifndef TCPV40HDRS
+
+#define V5_FD_SETSIZE      64
+#define V5_FD_SET(fd, set) do { \
+   /* if (((v5_fd_set *)(set))->fd_count < V5_FD_SETSIZE) - calcsize fixes this */ \
+        ((v5_fd_set *)(set))->fd_array[((v5_fd_set *)(set))->fd_count++]=fd;\
+} while(0)
+#define V5_FD_ISSET(fd, set)    v5_isset(fd, set)
+
+#endif
+
+
+/*******************************************************************************
+*   Structures and Typedefs                                                    *
+*******************************************************************************/
+/** Socket handle.*/
+typedef struct __libc_SocketHandle
+{
+    /** the common fh core. */
+    LIBCFH      core;
+
+    /** OS/2 socket number. */
+    int         iSocket;
+} LIBCSOCKETFH, *PLIBCSOCKETFH;
+
+
+#ifndef TCPV40HDRS
+
+#pragma pack(4)
+/** OS/2 oddities from the BSD 4.4 stack. */
+typedef struct v5_fd_set {
+        u_short fd_count;               /* how many are SET? */
+        int     fd_array[V5_FD_SETSIZE];/* an array of SOCKETs */
+} v5_fd_set;
+#pragma pack()
+
+/** internal helper */
+static inline int v5_isset(int fd, const struct v5_fd_set *set)
+{
+    const int  *pfd = &set->fd_array[0];
+    int         c   = set->fd_count;
+    while (c > 0)
+    {
+        if (*pfd == fd)
+            return 1;
+        pfd++;
+        c--;
+    }
+    return 0;
+}
+
+#endif
+
+
+/*******************************************************************************
+*   Global Variables                                                           *
+*******************************************************************************/
+extern LIBCFHOPS __libsocket_gSocketOps;
+
+
+/*******************************************************************************
+*   Internal Functions                                                         *
+*******************************************************************************/
+/** @defgroup libsocket_renamed     Renamed Imports.
+ * @{ */
+int     TCPCALL __libsocket_accept(int, struct sockaddr *, int *);
+int     TCPCALL __libsocket_bind(int, __const__ struct sockaddr *, int);
+int     TCPCALL __libsocket_connect(int, __const__ struct sockaddr *, int);
+int     TCPCALL __libsocket_getpeername(int, struct sockaddr *, int *);
+int     TCPCALL __libsocket_getsockname(int, struct sockaddr *, int *);
+int     TCPCALL __libsocket_getsockopt(int, int, int, void *, int *);
+int     TCPCALL __libsocket_ioctl(int, int, char *);
+int     TCPCALL __libsocket_listen(int, int);
+int     TCPCALL __libsocket_os2_ioctl(int, unsigned long, char *, int);
+int     TCPCALL __libsocket_os2_select(int *, int, int, int, long);
+int     TCPCALL __libsocket_recv(int, void *, int, int);
+int     TCPCALL __libsocket_recv(int, void *, int, int);
+int     TCPCALL __libsocket_recvfrom(int, void *, int, int, struct sockaddr *, int *);
+int     TCPCALL __libsocket_recvmsg(int, struct msghdr *, int);
+int     TCPCALL __libsocket_send(int, __const__ void *, int, int);
+int     TCPCALL __libsocket_send(int, const void *, int, int);
+int     TCPCALL __libsocket_sendmsg(int, __const__ struct msghdr *, int);
+int     TCPCALL __libsocket_sendto(int, __const__ void *, int, int, __const__ struct sockaddr *, int);
+int     TCPCALL __libsocket_setsockopt(int, int, int, __const__ void *, int);
+int     TCPCALL __libsocket_shutdown(int, int);
+int     TCPCALL __libsocket_sock_errno(void);
+int     TCPCALL __libsocket_socket(int, int, int);
+int     TCPCALL __libsocket_socket(int, int, int);
+int     TCPCALL __libsocket_socketpair(int, int, int, int *);
+int     TCPCALL __libsocket_soclose(int);
+int     TCPCALL __libsocket_tcpipv4_ioctl(int, unsigned long, char *, int);
+int     TCPCALL __libsocket_accept_and_recv(long, long*, struct sockaddr *, long*, struct sockaddr*, long*, caddr_t, size_t);
+void    TCPCALL __libsocket_addsockettolist(int);
+int     TCPCALL __libsocket_removesocketfromlist(int);
+ssize_t TCPCALL __libsocket_so_readv(int, struct iovec *, int);
+ssize_t TCPCALL __libsocket_so_writev(int, struct iovec *, int);
+int     TCPCALL __libsocket_so_cancel(int);
+int     TCPCALL __libsocket_soabort(int);
+int     TCPCALL __libsocket_Raccept(int, struct sockaddr *, int *);
+struct sockaddr_in;
+int     TCPCALL __libsocket_tcpip4_Rbind(int, struct sockaddr_in *, int, struct sockaddr_in *);
+int     TCPCALL __libsocket_Rbind(int, struct sockaddr *, int, struct sockaddr *);
+int     TCPCALL __libsocket_Rconnect(int, const struct sockaddr *, int);
+int     TCPCALL __libsocket_Rgetsockname(int, struct sockaddr *, int *);
+int     TCPCALL __libsocket_Rlisten(int, int);
+#ifndef TCPV40HDRS
+ssize_t TCPCALL __libsocket_send_file(int *, struct sf_parms *, int );
+#endif
+char *  TCPCALL __libsocket_sock_strerror(int);
+int     TCPCALL __libsocket_pair(int af, int type, int flags, int *osfd);
+#ifdef TCPV40HDRS
+int     TCPCALL __libsocket_bsdselect(int, fd_set *, fd_set *, fd_set *, struct timeval *);
+#else
+int     TCPCALL __libsocket_bsdselect(int, v5_fd_set *, v5_fd_set *, v5_fd_set *, struct timeval *);
+#endif
+void    TCPCALL __libsocket_set_errno(int);
+/** @} */
+
+
+/** @defgroup libsocket_internal    Internal helpers.
+ * @{ */
+/**
+ * Sets the LIBC and socket errno variables to a given error number.
+ */
+static inline void  __libsocket_setErrno(int err)
+{
+    errno = err;
+    __libsocket_set_errno(err + EOS2_TCPIP_OFFSET);
+}
+
+/**
+ * Updates the LIBC errno with the latest socket error.
+ */
+static inline void  __libsocket_setLibcErrno(void)
+{
+    int err = __libsocket_sock_errno();
+    if (err >= EOS2_TCPIP_OFFSET && err < EOS2_TCPIP_OFFSET + 1000)
+        errno = err - EOS2_TCPIP_OFFSET;
+}
+
+/**
+ * Updates the socket errno with the latest LIBC error.
+ */
+static inline void  __libsocket_setSocketErrno(void)
+{
+    __libsocket_set_errno(errno + EOS2_TCPIP_OFFSET);
+}
+
+/**
+ * Gets the socket errno translating it to a LIBC errno.
+ */
+static inline int   __libsocket_getSocketErrno(void)
+{
+    int err = __libsocket_sock_errno();
+    if (err >= EOS2_TCPIP_OFFSET && err < EOS2_TCPIP_OFFSET + 1000)
+        return err - EOS2_TCPIP_OFFSET;
+    return EDOOFUS;
+}
+
+
+/**
+ * Retrieve the socket handle structure for a given handle.
+ * @returns Pointer to socket handle structure on success.
+ * @returns NULL on failure with errno set to the appropriate value.
+ * @param   socket  Socket handle number.
+ */
+static inline PLIBCSOCKETFH   __libsocket_FH(int socket)
+{
+    PLIBCSOCKETFH   pFHSocket = (PLIBCSOCKETFH)__libc_FH(socket);
+    if (pFHSocket)
+    {
+        if ((pFHSocket->core.fFlags & F_TYPEMASK) == F_SOCKET)
+            return pFHSocket;
+        __libsocket_setErrno(ENOSYS);
+    }
+    else
+        __libsocket_setErrno(EBADF);
+    return NULL;
+}
+
+/** @} */
+
+
+/** @} */
+
+#endif
Index: trunk/src/emx/src/lib/socket/socket.imp
===================================================================
--- trunk/src/emx/src/lib/socket/socket.imp	(revision 885)
+++ trunk/src/emx/src/lib/socket/socket.imp	(revision 886)
@@ -5,54 +5,31 @@
 ;
 ; -------- tcpip32.lib --------
-accept                  tcpip32    1 ?
-_accept                 tcpip32    1 ?
-bind                    tcpip32    2 ?
-_bind                   tcpip32    2 ?
-connect                 tcpip32    3 ?
-_connect                tcpip32    3 ?
+__libsocket_accept      tcpip32    1 ?
+__libsocket_bind        tcpip32    2 ?
+__libsocket_connect     tcpip32    3 ?
 gethostid               tcpip32    4 ?
 _gethostid              tcpip32    4 ?
-getpeername             tcpip32    5 ?
-_getpeername            tcpip32    5 ?
-getsockname             tcpip32    6 ?
-_getsockname            tcpip32    6 ?
-getsockopt              tcpip32    7 ?
-_getsockopt             tcpip32    7 ?
-so_ioctl                tcpip32    8 ?
-_so_ioctl               tcpip32    8 ?
-listen                  tcpip32    9 ?
-_listen                 tcpip32    9 ?
-recv                    tcpip32   10 ?
-_recv                   tcpip32   10 ?
-recvfrom                tcpip32   11 ?
-_recvfrom               tcpip32   11 ?
-os2_select              tcpip32   12 ?
-_os2_select             tcpip32   12 ?
-send                    tcpip32   13 ?
-_send                   tcpip32   13 ?
-sendto                  tcpip32   14 ?
-_sendto                 tcpip32   14 ?
-setsockopt              tcpip32   15 ?
-_setsockopt             tcpip32   15 ?
-socket                  tcpip32   16 ?
-_socket                 tcpip32   16 ?
-soclose                 tcpip32   17 ?
-_soclose                tcpip32   17 ?
-so_cancel               tcpip32   18 ?
-_so_cancel              tcpip32   18 ?
-soabort                 tcpip32   19 ?
-_soabort                tcpip32   19 ?
+__libsocket_getpeername tcpip32    5 ?
+__libsocket_getsockname tcpip32    6 ?
+__libsocket_getsockopt  tcpip32    7 ?
+__libsocket_ioctl       tcpip32    8 ?
+__libsocket_listen      tcpip32    9 ?
+__libsocket_recv        tcpip32   10 ?
+__libsocket_recvfrom    tcpip32   11 ?
+__libsocket_os2_select  tcpip32   12 ?
+__libsocket_send        tcpip32   13 ?
+__libsocket_sendto      tcpip32   14 ?
+__libsocket_setsockopt  tcpip32   15 ?
+__libsocket_socket      tcpip32   16 ?
+__libsocket_soclose     tcpip32   17 ?
+__libsocket_so_cancel   tcpip32   18 ?
+__libsocket_soabort     tcpip32   19 ?
 os2_sock_errno          tcpip32   20 ?
-_os2_sock_errno        tcpip32   20 ?
-recvmsg                 tcpip32   21 ?
-_recvmsg                tcpip32   21 ?
-sendmsg                 tcpip32   22 ?
-_sendmsg                tcpip32   22 ?
-so_readv                tcpip32   23 ?
-_so_readv               tcpip32   23 ?
-so_writev               tcpip32   24 ?
-_so_writev              tcpip32   24 ?
-shutdown                tcpip32   25 ?
-_shutdown               tcpip32   25 ?
+_os2_sock_errno         tcpip32   20 ?
+__libsocket_recvmsg     tcpip32   21 ?
+__libsocket_sendmsg     tcpip32   22 ?
+__libsocket_so_readv    tcpip32   23 ?
+__libsocket_so_writev   tcpip32   24 ?
+__libsocket_shutdown    tcpip32   25 ?
 sock_init               tcpip32   26 ?
 _sock_init              tcpip32   26 ?
@@ -67,7 +44,6 @@
 getinetversion          tcpip32   31 ?
 _getinetversion         tcpip32   31 ?
-v5_bsdselect            tcpip32   32 ?
-set_errno               tcpip32   35 ?
-_set_errno              tcpip32   35 ?
+__libsocket_bsdselect   tcpip32   32 ?
+__libsocket_set_errno   tcpip32   35 ?
 winsockcleanupsockets   tcpip32   38 ?
 _winsockcleanupsockets  tcpip32   38 ?
@@ -174,6 +150,5 @@
 Rgethostbyname          tcpip32  161 ?
 _Rgethostbyname         tcpip32  161 ?
-os2_ioctl               tcpip32  200 ?
-_os2_ioctl              tcpip32  200 ?
+__libsocket_os2_ioctl   tcpip32  200 ?
 __TCPFDIsSet            tcpip32  201 ?
 ___TCPFDIsSet           tcpip32  201 ?
@@ -282,6 +257,5 @@
 inet_nsap_ntoa          tcpip32  253 ?
 _inet_nsap_ntoa         tcpip32  253 ?
-socketpair              tcpip32  254 ?
-_socketpair             tcpip32  254 ?
+__libsocket_socketpair  tcpip32  254 ?
 sock_strerror           tcpip32  255 ?
 _sock_strerror          tcpip32  255 ?
Index: trunk/src/emx/src/lib/socket/socket.smak
===================================================================
--- trunk/src/emx/src/lib/socket/socket.smak	(revision 885)
+++ trunk/src/emx/src/lib/socket/socket.smak	(revision 886)
@@ -3,12 +3,15 @@
 .MODULE	:= libsocket
 .MDESC	:= The socket library
-_MDIRS  := $.aout/tcpipv4 $.aout-prof/tcpipv4 $.aout/dep-tcpipv4 $.aout-prof/dep-tcpipv4 
-.MDEP   := emxomf emximp $(_MDIRS)
-TARGDIRS += $(_MDIRS)
+#_MDIRS  := $.aout/tcpipv4 $.aout-prof/tcpipv4 $.aout/dep-tcpipv4 $.aout-prof/dep-tcpipv4
+.MDEP   := emxomf emximp
+#$(_MDIRS)
+#TARGDIRS += $(_MDIRS)
 
 .TARGET	:= libsocket_p.a
 .TKIND	:= aout prof
-.TSRC   := $(wildcard src/lib/socket/386/*.s)
-.TSRC   += src/lib/socket/sock_errno.c src/lib/socket/bsdselect.c
+.TSRC   := $(wildcard src/lib/socket/386/*.s) \
+           $(filter-out src/lib/socket/aliases/herror%, $(wildcard src/lib/socket/aliases/*.s)) \
+           $(filter-out src/lib/socket/herror%, $(wildcard src/lib/socket/*.c))
+.TCF    := -I$.
 .IMPS   := src/lib/socket/socket.imp
 .INSDIR	:= lib/
@@ -18,32 +21,22 @@
 .TARGET	:= libsocket.a
 .TKIND	:= aout
-.TSRC   := $(wildcard src/lib/socket/386/*.s)
-.TSRC   += src/lib/socket/sock_errno.c src/lib/socket/bsdselect.c
-.IMPS   := src/lib/socket/socket.imp
-.INSDIR	:= lib/
 .TKEEP	:= 1
 include mkimplib.smak
 
 	
-.TARGET	:= tcpipv4/libsocket_p.a
+.TARGET	:= libsocket_p.a
 .TKIND	:= aout prof
-.TSRC   := $(wildcard src/lib/socket/386/*.s)
-.TSRC	+= src/lib/socket/herror.c src/lib/socket/sock_errno.c src/lib/socket/socketpair.c
-.TSRC	+= src/lib/socket/socketpair-alias.s
+.TKVAR	:= tcpipv4
+.TSRC   := $(wildcard src/lib/socket/386/*.s) $(wildcard src/lib/socket/aliases/*.s) \
+           $(wildcard src/lib/socket/*.c)
+.TCF    := -I$. -DTCPV40HDRS
 .IMPS   := src/lib/socket/socket_tcpipv4.imp
-.INSDIR	:= lib/
+.INSDIR	:= lib/tcpipv4
 .TKEEP	:= 1
 include mkimplib.smak
 
-.TARGET	:= tcpipv4/libsocket.a
+.TARGET	:= libsocket.a
 .TKIND	:= aout
-.TSRC   := $(wildcard src/lib/socket/386/*.s)
-.TSRC	+= src/lib/socket/herror.c src/lib/socket/sock_errno.c src/lib/socket/socketpair.c
-.TSRC	+= src/lib/socket/socketpair-alias.s
-.IMPS   := src/lib/socket/socket_tcpipv4.imp
-.TKEEP	:= 1
 include mkimplib.smak
-
-#$.omf/tcpipv4 omf-prof/tcpipv4
 
 
Index: trunk/src/emx/src/lib/socket/socket_tcpipv4.imp
===================================================================
--- trunk/src/emx/src/lib/socket/socket_tcpipv4.imp	(revision 885)
+++ trunk/src/emx/src/lib/socket/socket_tcpipv4.imp	(revision 886)
@@ -5,54 +5,31 @@
 ;
 ; -------- so32dll.lib --------
-accept                  so32dll    1 ?
-_accept                 so32dll    1 ?
-bind                    so32dll    2 ?
-_bind                   so32dll    2 ?
-connect                 so32dll    3 ?
-_connect                so32dll    3 ?
+__libsocket_accept      so32dll    1 ?
+__libsocket_bind        so32dll    2 ?
+__libsocket_connect     so32dll    3 ?
 gethostid               so32dll    4 ?
 _gethostid              so32dll    4 ?
-getpeername             so32dll    5 ?
-_getpeername            so32dll    5 ?
-getsockname             so32dll    6 ?
-_getsockname            so32dll    6 ?
-getsockopt              so32dll    7 ?
-_getsockopt             so32dll    7 ?
-os2_ioctl               so32dll    8 ?
-_os2_ioctl              so32dll    8 ?
-listen                  so32dll    9 ?
-_listen                 so32dll    9 ?
-recv                    so32dll   10 ?
-_recv                   so32dll   10 ?
-recvfrom                so32dll   11 ?
-_recvfrom               so32dll   11 ?
-os2_select              so32dll   12 ?
-_os2_select             so32dll   12 ?
-send                    so32dll   13 ?
-_send                   so32dll   13 ?
-sendto                  so32dll   14 ?
-_sendto                 so32dll   14 ?
-setsockopt              so32dll   15 ?
-_setsockopt             so32dll   15 ?
-socket                  so32dll   16 ?
-_socket                 so32dll   16 ?
-soclose                 so32dll   17 ?
-_soclose                so32dll   17 ?
-so_cancel               so32dll   18 ?
-_so_cancel              so32dll   18 ?
-soabort                 so32dll   19 ?
-_soabort                so32dll   19 ?
+__libsocket_getpeername so32dll    5 ?
+__libsocket_getsockname so32dll    6 ?
+__libsocket_getsockopt  so32dll    7 ?
+__libsocket_os2_ioctl   so32dll    8 ?
+__libsocket_listen      so32dll    9 ?
+__libsocket_recv        so32dll   10 ?
+__libsocket_recvfrom    so32dll   11 ?
+__libsocket_os2_select  so32dll   12 ?
+__libsocket_send        so32dll   13 ?
+__libsocket_sendto      so32dll   14 ?
+__libsocket_setsockopt  so32dll   15 ?
+__libsocket_socket      so32dll   16 ?
+__libsocket_soclose     so32dll   17 ?
+__libsocket_so_cancel   so32dll   18 ?
+__libsocket_soabort     so32dll   19 ?
 os2_sock_errno          so32dll   20 ?
 _os2_sock_errno         so32dll   20 ?
-recvmsg                 so32dll   21 ?
-_recvmsg                so32dll   21 ?
-sendmsg                 so32dll   22 ?
-_sendmsg                so32dll   22 ?
-so_readv                so32dll   23 ?
-_so_readv               so32dll   23 ?
-so_writev               so32dll   24 ?
-_so_writev              so32dll   24 ?
-shutdown                so32dll   25 ?
-_shutdown               so32dll   25 ?
+__libsocket_recvmsg     so32dll   21 ?
+__libsocket_sendmsg     so32dll   22 ?
+__libsocket_so_readv    so32dll   23 ?
+__libsocket_so_writev   so32dll   24 ?
+__libsocket_shutdown    so32dll   25 ?
 sock_init               so32dll   26 ?
 _sock_init              so32dll   26 ?
@@ -65,8 +42,6 @@
 getinetversion          so32dll   31 ?
 _getinetversion         so32dll   31 ?
-bsdselect               so32dll   32 ?
-_bsdselect              so32dll   32 ?
-set_errno               so32dll   35 ?
-_set_errno              so32dll   35 ?
+__libsocket_bsdselect   so32dll   32 ?
+__libsocket_set_errno   so32dll   35 ?
 getsocketfromlist       so32dll   39 ?
 _getsocketfromlist      so32dll   39 ?
Index: trunk/src/emx/src/lib/socket/socketops.c
===================================================================
--- trunk/src/emx/src/lib/socket/socketops.c	(revision 886)
+++ trunk/src/emx/src/lib/socket/socketops.c	(revision 886)
@@ -0,0 +1,533 @@
+/* $Id$ */
+/** @file
+ *
+ * socket handle operations.
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#define INCL_TCPIP_ALLIOCTLS
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/sockio.h>
+#include <net/if.h>
+#include <net/if_arp.h>
+#include <netinet/in_systm.h>
+#include <netinet/in.h>
+#include <netinet/if_ether.h>
+#include <netinet/ip.h>
+#include <netinet/ip_var.h>
+#include <netinet/ip_icmp.h>
+#include <netinet/tcp.h>
+#include <netinet/tcp_var.h>
+#include <netinet/udp.h>
+#include <netinet/udp_var.h>
+#include <net/route.h>
+
+#include <sys/fcntl.h>
+#include <sys/filio.h>
+#include <emx/io.h>
+#include "socket.h"
+
+
+/*******************************************************************************
+*   Structures and Typedefs                                                    *
+*******************************************************************************/
+#ifdef TCPV40HDRS
+/* not included in the TCPV40HDRS - this is from the BSD4.4 stack headers
+                                    and doesn't have to right!!! */
+struct arpreq_tr {
+        struct  sockaddr arp_pa;                /* protocol address */
+        struct  sockaddr arp_ha;                /* hardware address */
+        long_int arp_flags;                     /* flags */
+        u_short arp_rcf;                        /* token ring routing control field */
+        u_short arp_rseg[8];                    /* token ring routing segments */
+};
+
+#define MAX_IN_MULTI      16*IP_MAX_MEMBERSHIPS      /* 320 max per os2 */
+struct  addrreq  {                              /* get multicast addresses */
+        char    ifr_name[IFNAMSIZ];
+        struct  sockaddr ifr_addrs;
+        u_long  maddr[MAX_IN_MULTI];
+};
+
+#pragma pack(1)
+struct  oarptab {
+        struct  in_addr at_iaddr;       /* internet address */
+        u_char  at_enaddr[6];           /* ethernet address */
+        u_char  at_timer;               /* minutes since last reference */
+        u_char  at_flags;               /* flags */
+#ifdef KERNEL
+        struct  mbuf *at_hold;          /* last packet until resolved/timeout */
+#else
+        void * at_hold;
+#endif
+        u_short at_rcf;                 /* token ring routing control field */
+        u_short at_rseg[8];             /* token ring routing segments */
+        u_long  at_millisec;            /* TOD milliseconds of last update */
+        u_short at_interface;           /* interface index */
+};
+#pragma pack()
+
+#pragma pack(1)
+struct  statatreq {
+        u_long addr;
+        short interface;
+        u_long mask;
+        u_long broadcast;
+};
+#pragma pack()
+
+#endif
+
+/*******************************************************************************
+*   Internal Functions                                                         *
+*******************************************************************************/
+static int __libsocket_ops_Close(PLIBCFH pFH, int fh);
+static int __libsocket_ops_Read(PLIBCFH pFH, int fh, void *pvBuf, size_t cbRead, size_t *pcbRead);
+static int __libsocket_ops_Write(PLIBCFH pFH, int fh, const void *pvBuf, size_t cbWrite, size_t *pcbWritten);
+static int __libsocket_ops_Duplicate(PLIBCFH pFH, int fh, int *pfhNew);
+static int __libsocket_ops_FileControl(PLIBCFH pFH, int fh, int iIOControl, int iArg, int *prc);
+static int __libsocket_ops_IOControl(PLIBCFH pFH, int fh, int iIOControl, int iArg, int *prc);
+
+
+/*******************************************************************************
+*   Global Variables                                                           *
+*******************************************************************************/
+/** Operations on sockets. */
+LIBCFHOPS __libsocket_gSocketOps =
+{
+    enmFH_Socket, /* Handle type. */
+    __libsocket_ops_Close,
+    __libsocket_ops_Read,
+    __libsocket_ops_Write,
+    __libsocket_ops_Duplicate,
+    __libsocket_ops_FileControl,
+    __libsocket_ops_IOControl
+};
+
+
+/** Close operation.
+ * @returns 0 on success.
+ * @returns OS/2 error code or negated errno on failure.
+ * @param   pFH         Pointer to the handle structure to operate on.
+ * @param   fh          It's associated filehandle.
+ */
+static int __libsocket_ops_Close(PLIBCFH pFH, int fh)
+{
+    PLIBCSOCKETFH   pSocketFH = (PLIBCSOCKETFH)pFH;
+    int             rc;
+    rc = __libsocket_soclose(pSocketFH->iSocket);
+    if (rc)
+        return -__libsocket_getSocketErrno();
+    return 0;
+}
+
+
+/** Read operation.
+ * @returns 0 on success.
+ * @returns OS/2 error code or negated errno on failure.
+ * @param   pFH         Pointer to the handle structure to operate on.
+ * @param   fh          It's associated filehandle.
+ * @param   pvBuf       Pointer to the buffer to read into.
+ * @param   cbRead      Number of bytes to read.
+ * @param   pcbRead     Where to store the count of bytes actually read.
+ */
+static int __libsocket_ops_Read(PLIBCFH pFH, int fh, void *pvBuf, size_t cbRead, size_t *pcbRead)
+{
+    PLIBCSOCKETFH   pSocketFH = (PLIBCSOCKETFH)pFH;
+    int             rc;
+
+    rc = __libsocket_recv(pSocketFH->iSocket, pvBuf, cbRead, 0);
+    if (rc < 0)
+    {
+        *pcbRead = 0;
+        return -__libsocket_getSocketErrno();
+    }
+
+    *pcbRead = rc;
+    return 0;
+}
+
+/** Write operation.
+ * @returns 0 on success.
+ * @returns OS/2 error code or negated errno on failure.
+ * @param   pFH         Pointer to the handle structure to operate on.
+ * @param   fh          It's associated filehandle.
+ * @param   pvBuf       Pointer to the buffer which contains the data to write.
+ * @param   cbWrite     Number of bytes to write.
+ * @param   pcbWritten  Where to store the count of bytes actually written.
+ */
+static int __libsocket_ops_Write(PLIBCFH pFH, int fh, const void *pvBuf, size_t cbWrite, size_t *pcbWritten)
+{
+    PLIBCSOCKETFH   pSocketFH = (PLIBCSOCKETFH)pFH;
+    int             rc;
+
+    rc = __libsocket_send(pSocketFH->iSocket, pvBuf, cbWrite, 0);
+    if (rc < 0)
+    {
+        *pcbWritten = 0;
+        return -__libsocket_getSocketErrno();
+    }
+
+    *pcbWritten = 0;
+    return 0;
+
+}
+
+
+/** Duplicate handle operation.
+ * @returns 0 on success, OS/2 error code on failure.
+ * @param   pFH         Pointer to the handle structure to operate on.
+ * @param   fh          It's associated filehandle.
+ * @param   pfhNew      Where to store the duplicate filehandle.
+ *                      The input value describe how the handle is to be
+ *                      duplicated. If it's -1 a new handle is allocated.
+ *                      Any other value will result in that value to be
+ *                      used as handle. Any existing handle with that
+ *                      value will be closed.
+ */
+static int __libsocket_ops_Duplicate(PLIBCFH pFH, int fh, int *pfhNew)
+{
+    return -ENOSYS;
+}
+
+
+/** File Control operation.
+ * @returns 0 on success.
+ * @returns OS/2 error code or negated errno on failure.
+ * @param   pFH         Pointer to the handle structure to operate on.
+ * @param   fh          It's associated filehandle.
+ * @param   iIOControl  Which file file control operation to perform.
+ * @param   iArg        Argument which content is specific to each
+ *                      iIOControl operation.
+ * @param   prc         Where to store the value which upon success is
+ *                      returned to the caller.
+ */
+static int __libsocket_ops_FileControl(PLIBCFH pFH, int fh, int iIOControl, int iArg, int *prc)
+{
+    /*PLIBCSOCKETFH   pSocketFH = (PLIBCSOCKETFH)pFH;*/
+    return -ENOSYS;
+}
+
+
+/** I/O Control operation.
+ * @returns 0 on success.
+ * @returns OS/2 error code or negated errno on failure.
+ * @param   pFH         Pointer to the handle structure to operate on.
+ * @param   fh          It's associated filehandle.
+ * @param   iIOControl  Which I/O control operation to perform.
+ * @param   iArg        Argument which content is specific to each
+ *                      iIOControl operation.
+ * @param   prc         Where to store the value which upon success is
+ *                      returned to the caller.
+ */
+static int __libsocket_ops_IOControl(PLIBCFH pFH, int fh, int iIOControl, int iArg, int *prc)
+{
+    PLIBCSOCKETFH   pSocketFH = (PLIBCSOCKETFH)pFH;
+    char           *pchArg = (char *)iArg;
+    int             rc;
+#ifndef TCPV40HDRS
+    switch (__IOCLW(iIOControl))
+    {
+        case SIOSTATARP:
+            rc = __libsocket_os2_ioctl(pSocketFH->iSocket, iIOControl, pchArg, sizeof(struct oarptab));
+            break;
+        case SIOSTATAT:
+            /** this isn't really suitable for this ioctl interface!! */
+            rc = __libsocket_os2_ioctl(pSocketFH->iSocket, iIOControl, pchArg, sizeof(struct statatreq) + 2);
+            break;
+        case SIOSTATIF:
+            rc = __libsocket_os2_ioctl(pSocketFH->iSocket, iIOControl, pchArg, sizeof(struct ifmib));
+            break;
+        case SIOSTATIF42:
+            /* What the h*** is the difference between the SIOSTATIF42 ioctl and SIOSTATIF?
+               The docs doesn't make sense when looking in the headers... */
+            rc = __libsocket_os2_ioctl(pSocketFH->iSocket, iIOControl, pchArg, sizeof(struct ifmib));
+            break;
+        case SIOSTATRT:
+            rc = __libsocket_os2_ioctl(pSocketFH->iSocket, iIOControl, pchArg, sizeof(struct rtentries));
+            break;
+        case SIOSTATSO:
+            /** this isn't really suitable for this ioctl interface!! */
+            rc = __libsocket_os2_ioctl(pSocketFH->iSocket, iIOControl, pchArg, sizeof(struct sockaddr));
+            break;
+
+        case FIONBIO:
+            rc = __libsocket_ioctl(pSocketFH->iSocket, iIOControl, pchArg);
+            if (rc)
+            {
+                if (*(unsigned*)pchArg)
+                    pSocketFH->core.fFlags |= O_NDELAY;
+                else
+                    pSocketFH->core.fFlags &= ~O_NDELAY;
+            }
+            break;
+
+        default:
+            rc = __libsocket_ioctl(pSocketFH->iSocket, iIOControl, pchArg);
+            break;
+    }
+
+#else
+    int     cb;
+
+    /*
+     * This ioctl interface requires a size, which thus is something we
+     * must determin. :-/
+     */
+    switch (__IOCLW(iIOControl))
+    {
+        case __IOCLW(FIONREAD):
+        case __IOCLW(FIOASYNC):
+        case __IOCLW(SIOCATMARK):
+        case __IOCLW(FIONBIO):
+        case __IOCLW(SIOCSHIWAT):
+        case __IOCLW(SIOCGHIWAT):
+        case __IOCLW(SIOCSLOWAT):
+        case __IOCLW(SIOCGLOWAT):
+        case __IOCLW(SIOCSPGRP):
+        case __IOCLW(SIOCGPGRP):
+            cb = sizeof(int);
+            break;
+
+        case __IOCLW(SIOCSHOSTID):
+        case __IOCLW(SIOCGNBNAME):
+        case __IOCLW(SIOCSNBNAME):
+        case __IOCLW(SIOCGNCBFN):
+            cb = sizeof(long);
+            break;
+
+        case __IOCLW(FIONSTATUS):
+            cb = sizeof(short) * 4;
+            break;
+
+        case __IOCLW(FIOBSTATUS):
+            cb = sizeof(short);
+            break;
+
+        case __IOCLW(SIOCGIFADDR):
+        case __IOCLW(SIOCGIFBRDADDR):
+        case __IOCLW(SIOCGIFDSTADDR):
+        case __IOCLW(SIOCGIFFLAGS):
+        case __IOCLW(SIOCGIFMETRIC):
+        case __IOCLW(SIOCGIFNETMASK):
+        case __IOCLW(SIOCSIFADDR):
+        case __IOCLW(SIOCSIFBRDADDR):
+        case __IOCLW(SIOCSIFDSTADDR):
+        case __IOCLW(SIOCSIFFLAGS):
+        case __IOCLW(SIOCSIFMETRIC):
+        case __IOCLW(SIOCSIFNETMASK):
+        case __IOCLW(SIOCSIFALLRTB):
+        case __IOCLW(SIOCSIF802_3):
+        case __IOCLW(SIOCSIFNO802_3):
+        case __IOCLW(SIOCSIFNOREDIR):
+        case __IOCLW(SIOCSIFYESREDIR):
+        case __IOCLW(SIOCSIFMTU):
+        case __IOCLW(SIOCSIFFDDI):
+        case __IOCLW(SIOCSIFNOFDDI):
+        case __IOCLW(SIOCSRDBRD):
+        case __IOCLW(SIOCADDMULTI):
+        case __IOCLW(SIOCDELMULTI):
+        case __IOCLW(SIOCMULTISBC):
+        case __IOCLW(SIOCMULTISFA):
+        case __IOCLW(SIOCDIFADDR):
+        case __IOCLW(SIOCGUNIT):
+        case __IOCLW(SIOCGIFVALID):
+        case __IOCLW(SIOCGIFEFLAGS):
+        case __IOCLW(SIOCSIFEFLAGS):
+        case __IOCLW(SIOCGIFTRACE):
+        case __IOCLW(SIOCSIFTRACE):
+        case __IOCLW(SIOCSSTAT):
+        case __IOCLW(SIOCGSTAT):
+            cb = sizeof(struct ifreq);
+            break;
+
+        case __IOCLW(SIOCADDRT):
+        case __IOCLW(SIOCDELRT):
+            cb = sizeof(struct rtentry);
+            break;
+
+        case __IOCLW(SIOCSARP):
+        case __IOCLW(SIOCGARP):
+        case __IOCLW(SIOCDARP):
+            cb = sizeof(struct arpreq);
+            break;
+
+        case __IOCLW(SIOCGIFCONF):
+            cb = sizeof(struct ifconf);
+            break;
+
+        case __IOCLW(SIOCSARP_TR):
+        case __IOCLW(SIOCGARP_TR):
+            cb = sizeof(struct arpreq_tr);
+            break;
+
+        /* duplicate! Bug in BSD 4.3 stack I'd say.
+        case __IOCLW(SIOCAIFADDR):
+            cb = sizeof(struct ifaliasreq);
+            break;
+         */
+
+        case __IOCLW(SIOCGIFBOUND):
+            cb = sizeof(struct bndreq);
+            break;
+
+        case __IOCLW(SIOCGMCAST):
+            cb = sizeof(struct addrreq);
+            break;
+
+        case __IOCLW(SIOSTATMBUF):
+            cb = sizeof(struct mbstat);
+            break;
+
+        case __IOCLW(SIOSTATTCP):
+            cb = sizeof(struct tcpstat);
+            break;
+
+        case __IOCLW(SIOSTATUDP):
+            cb = sizeof(struct udpstat);
+            break;
+
+        case __IOCLW(SIOSTATIP):
+            cb = sizeof(struct ipstat);
+            break;
+
+        case __IOCLW(SIOSTATSO):
+            cb = sizeof(struct sockaddr);
+            break;
+
+        case __IOCLW(SIOSTATRT):
+            cb = sizeof(struct rtentries);
+            break;
+
+        case __IOCLW(SIOFLUSHRT):
+            cb = sizeof(long);
+            break;
+
+        case __IOCLW(SIOSTATICMP):
+            cb = sizeof(struct icmpstat);
+            break;
+
+        case __IOCLW(SIOSTATIF):
+            cb = sizeof(struct ifmib);
+            break;
+
+        case __IOCLW(SIOSTATAT):
+            /** this isn't really suitable for this ioctl interface!! */
+            cb = sizeof(struct statatreq) + 2;
+            break;
+
+        case __IOCLW(SIOSTATARP):
+            cb = sizeof(struct oarptab);
+            break;
+
+        case __IOCLW(SIOSTATIF42):
+            cb = sizeof(struct ifmib); /* docs are unclear here */
+            break;
+
+        case __IOCLW(SIOSTATCNTRT):
+            cb = sizeof(int);
+            break;
+
+        case __IOCLW(SIOSTATCNTAT):
+            cb = sizeof(int);
+            break;
+
+        default:
+            cb = -1;
+            break;
+
+/** @todo There is a bunch of IOCtls in the BSD 4.3 stack
+ *        for which I don't have the docs and don't know exact
+ *        what sizes to use:
+ *               FIOTCPCKSUM     ioc('f', 128)
+ *               FIONURG         ioc('f', 121)
+ *               SIOMETRIC1RT    ioc('r', 12)
+ *               SIOMETRIC2RT    ioc('r', 13)
+ *               SIOMETRIC3RT    ioc('r', 14)
+ *               SIOMETRIC4RT    ioc('r', 15)
+ *               SIOCREGADDNET   ioc('r', 12)
+ *               SIOCREGDELNET   ioc('r', 13)
+ *               SIOCREGROUTES   ioc('r', 14)
+ *               SIOCFLUSHROUTES ioc('r', 15)
+ *               SIOCSIFSETSIG   ioc('i', 25)
+ *               SIOCSIFCLRSIG   ioc('i', 26)
+ *               SIOCSIFBRD      ioc('i', 27)
+ *               SIOCGIFLOAD     ioc('i', 27)
+ *               SIOCSIFFILTERSRC ioc('i', 28)
+ *               SIOCGIFFILTERSRC ioc('i',29)
+ *               SIOCSIFSNMPSIG  ioc('i', 33)
+ *               SIOCSIFSNMPCLR  ioc('i', 34)
+ *               SIOCSIFSNMPCRC  ioc('i', 35)
+ *               SIOCSIFPRIORITY ioc('i', 36)
+ *               SIOCGIFPRIORITY ioc('i', 37)
+ *               SIOCSIFFILTERDST ioc('i', 38)
+ *               SIOCGIFFILTERDST ioc('i',39)
+ *               SIOCSIFSPIPE     ioc('i',71)
+ *               SIOCSIFRPIPE     ioc('i',72)
+ *               SIOCSIFTCPSEG   ioc('i',73)
+ *               SIOCSIFUSE576   ioc('i',74)
+ */
+    } /* size switch */
+
+    /*
+     * Do the call.
+     */
+    if (cb >= 0)
+    {
+        switch (__IOCLW(iIOControl))
+        {
+            case FIONBIO:
+                rc = __libsocket_tcpipv4_ioctl(pSocketFH->iSocket, iIOControl, pchArg, cb);
+                if (rc)
+                {
+                    if (*(unsigned*)pchArg)
+                        pSocketFH->core.fFlags |= O_NDELAY;
+                    else
+                        pSocketFH->core.fFlags &= ~O_NDELAY;
+                }
+                break;
+
+            default:
+                rc = __libsocket_tcpipv4_ioctl(pSocketFH->iSocket, iIOControl, pchArg, cb);
+                break;
+        }
+    }
+    else
+    {   /*  */
+        *prc = -1;
+        return -ENOSYS;
+    }
+
+#endif
+
+    *prc = rc;
+    if (rc < 0)
+        return -__libsocket_getSocketErrno();
+    return 0;
+
+    return -ENOSYS;
+}
Index: trunk/src/emx/src/lib/socket/socketpair.c
===================================================================
--- trunk/src/emx/src/lib/socket/socketpair.c	(revision 885)
+++ trunk/src/emx/src/lib/socket/socketpair.c	(revision 886)
@@ -12,5 +12,9 @@
  */
 
-#define TCPV40HDRS
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
 #include <sys/types.h>
 #include <sys/time.h>
@@ -20,4 +24,9 @@
 #include <stdio.h>
 #include <string.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
 
 
@@ -35,4 +44,5 @@
 int     TCPCALL socketpair(int af, int type, int flags, int *osfd)
 {
+#ifdef TCPV40HDRS
     int     rc;
     int     sock1 = -1;
@@ -106,4 +116,40 @@
         soclose(sock2);
     return -1;
+
+#else
+    int             rc;
+    int             fh1;
+    int             fh2;
+    PLIBCSOCKETFH   pFH;
+
+    rc = __libsocket_pair(af, type, flags, osfd);
+    if (rc < 0)
+    {
+        __libsocket_setLibcErrno();
+        return -1;
+    }
+
+    /*
+     * Allocate LIBC sockets.
+     */
+    rc = __libc_FHAllocate(-1, O_RDWR | F_SOCKET, sizeof(LIBCSOCKETFH), &__libsocket_gSocketOps, (PLIBCFH*)&pFH, &fh1);
+    if (!rc)
+    {
+        pFH->iSocket = osfd[0];
+        rc = __libc_FHAllocate(-1, O_RDWR | F_SOCKET, sizeof(LIBCSOCKETFH), &__libsocket_gSocketOps, (PLIBCFH*)&pFH, &fh2);
+        if (!rc)
+        {
+            pFH->iSocket = osfd[1];
+            osfd[0] = fh1;
+            osfd[1] = fh2;
+            return 0;
+        }
+        __libc_FHClose(fh1);
+    }
+
+    __libsocket_soclose(osfd[0]);
+    __libsocket_soclose(osfd[1]);
+    return -1;
+#endif
 }
 
Index: trunk/src/emx/src/lib/socket/soclose.c
===================================================================
--- trunk/src/emx/src/lib/socket/soclose.c	(revision 886)
+++ trunk/src/emx/src/lib/socket/soclose.c	(revision 886)
@@ -0,0 +1,49 @@
+/* $Id$ */
+/** @file
+ *
+ * soclose().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+int soclose(int socket)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        if (!__libc_FHClose(socket))
+            return 0;
+        __libsocket_setSocketErrno();
+    }
+
+    return -1;
+}
+
Index: trunk/src/emx/src/libsocket/_getsockhandle.c
===================================================================
--- trunk/src/emx/src/libsocket/_getsockhandle.c	(revision 886)
+++ trunk/src/emx/src/libsocket/_getsockhandle.c	(revision 886)
@@ -0,0 +1,51 @@
+/* $Id$ */
+/** @file
+ *
+ * _getsockhandle()
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <emx/io.h>
+#include "socket.h"
+
+
+/**
+ * Gets the OS/2 socket handle.
+ *
+ * @returns OS/2 socket handle.
+ * @returns -1 on failure and errno set to EBADF or BNOSYS.
+ * @param   socket  LIBC socket handle.
+ */
+int _getsockhandle(int socket)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+        return pFHSocket->iSocket;
+    return -1;
+}
Index: trunk/src/emx/src/libsocket/_impsockhandle.c
===================================================================
--- trunk/src/emx/src/libsocket/_impsockhandle.c	(revision 886)
+++ trunk/src/emx/src/libsocket/_impsockhandle.c	(revision 886)
@@ -0,0 +1,74 @@
+/* $Id$ */
+/** @file
+ *
+ * _getsockhandle()
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+
+/**
+ * Imports an socket handle from the OS/2 handle space.
+ *
+ * @returns LIBC socket handle.
+ * @returns -1 on failure and errno set to something sensible.
+ * @param   os2socket   OS/2 socket handle.
+ * @param   flags       Reserved for the future by EM - ignored.
+ */
+int _impsockhandle(int os2socket, int flags)
+{
+    PLIBCSOCKETFH   pFH;
+    int             iType;
+    int             cb;
+    int             fh;
+    int             rc;
+
+    /*
+     * Verify the socket.
+     */
+    cb = sizeof(iType);
+    if (__libsocket_getsockopt(os2socket, SOL_SOCKET, SO_TYPE, (char*)&iType, &cb) < 0)
+    {
+        __libsocket_setErrno(EBADF);
+        return -1;
+    }
+
+    /*
+     * Allocate LIBC handle.
+     */
+    rc = __libc_FHAllocate(-1, O_RDWR | F_SOCKET, sizeof(LIBCSOCKETFH), &__libsocket_gSocketOps, (PLIBCFH*)&pFH, &fh);
+    if (rc)
+        return -1;
+    pFH->iSocket = os2socket;
+    return fh;
+}
+
Index: trunk/src/emx/src/libsocket/accept.c
===================================================================
--- trunk/src/emx/src/libsocket/accept.c	(revision 885)
+++ trunk/src/emx/src/libsocket/accept.c	(revision 886)
@@ -1,23 +1,65 @@
-/* accept.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
+/* $Id: $ */
+/** @file
+ *
+ * accept().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
 #include <sys/fcntl.h>
-#include <errno.h>
 #include <emx/io.h>
-#include <emx/syscalls.h>
+#include "socket.h"
 
-int accept (int handle, struct sockaddr *addr, int *paddrlen)
+int accept(int socket, struct sockaddr *addr, int *addrlen)
 {
-  int s, *pflags;
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    int             s;
+    if (pFHSocket)
+    {
+        s = __libsocket_accept(pFHSocket->iSocket, addr, addrlen);
+        if (s >= 0)
+        {
+            int             rc;
+            int             fh;
+            PLIBCSOCKETFH   pFH;
+            rc = __libc_FHAllocate(-1, O_RDWR | F_SOCKET, sizeof(LIBCSOCKETFH), &__libsocket_gSocketOps, (PLIBCFH*)&pFH, &fh);
+            if (!rc)
+            {
+                pFH->iSocket = s;
+                return fh;
+            }
+            else
+                __libsocket_soclose(s);
+            __libsocket_setSocketErrno();
+        }
+        else
+            __libsocket_setLibcErrno();
+    }
 
-  s = __accept (handle, addr, paddrlen);
-  if (s < 0)
     return -1;
-  if ((pflags = _fd_init (s)) == NULL)
-    {
-      __close (s);
-      errno = EMFILE;
-      return -1;
-    }
-  *pflags = O_RDWR | F_SOCKET;
-  return s;
 }
+
Index: trunk/src/emx/src/libsocket/aliases/.readme
===================================================================
--- trunk/src/emx/src/libsocket/aliases/.readme	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/.readme	(revision 886)
@@ -0,0 +1,3 @@
+This directory contains __cdecl to _System aliases for libsocket functions
+which we wrap or implement. The libsocket functions which are imported
+directly have these aliases in the .imp files.
Index: trunk/src/emx/src/libsocket/aliases/accept-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/accept-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/accept-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_accept",11,0,0,0;.stabs "accept",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/bind-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/bind-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/bind-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_bind",11,0,0,0;.stabs "bind",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/bsdselect-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/bsdselect-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/bsdselect-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_bsdselect",11,0,0,0;.stabs "bsdselect",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/connect-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/connect-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/connect-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_connect",11,0,0,0;.stabs "connect",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/getpeername-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/getpeername-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/getpeername-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_getpeername",11,0,0,0;.stabs "getpeername",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/getsockname-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/getsockname-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/getsockname-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_getsockname",11,0,0,0;.stabs "getsockname",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/getsockopt-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/getsockopt-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/getsockopt-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_getsockopt",11,0,0,0;.stabs "getsockopt",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/herror-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/herror-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/herror-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_herror",11,0,0,0;.stabs "herror",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/inetaddr-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/inetaddr-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/inetaddr-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_inetaddr",11,0,0,0;.stabs "inetaddr",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/inetnetw-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/inetnetw-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/inetnetw-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_inetnetw",11,0,0,0;.stabs "inetnetw",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/listen-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/listen-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/listen-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_listen",11,0,0,0;.stabs "listen",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/recv-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/recv-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/recv-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_recv",11,0,0,0;.stabs "recv",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/recvfrom-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/recvfrom-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/recvfrom-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_recvfrom",11,0,0,0;.stabs "recvfrom",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/recvmsg-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/recvmsg-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/recvmsg-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_recvmsg",11,0,0,0;.stabs "recvmsg",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/send-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/send-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/send-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_send",11,0,0,0;.stabs "send",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/sendmsg-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/sendmsg-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/sendmsg-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_sendmsg",11,0,0,0;.stabs "sendmsg",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/sendto-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/sendto-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/sendto-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_sendto",11,0,0,0;.stabs "sendto",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/setsockopt-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/setsockopt-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/setsockopt-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_setsockopt",11,0,0,0;.stabs "setsockopt",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/shutdown-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/shutdown-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/shutdown-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_shutdown",11,0,0,0;.stabs "shutdown",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/so_cancel-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/so_cancel-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/so_cancel-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_so_cancel",11,0,0,0;.stabs "so_cancel",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/so_ioctl-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/so_ioctl-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/so_ioctl-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_so_ioctl",11,0,0,0;.stabs "so_ioctl",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/so_readv-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/so_readv-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/so_readv-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_so_readv",11,0,0,0;.stabs "so_readv",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/so_writev-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/so_writev-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/so_writev-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_so_writev",11,0,0,0;.stabs "so_writev",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/soabort-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/soabort-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/soabort-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_soabort",11,0,0,0;.stabs "soabort",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/sock_errno-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/sock_errno-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/sock_errno-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_sock_errno",11,0,0,0;.stabs "sock_errno",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/socket-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/socket-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/socket-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_socket",11,0,0,0;.stabs "socket",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/socketops-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/socketops-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/socketops-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_socketops",11,0,0,0;.stabs "socketops",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/socketpair-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/socketpair-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/socketpair-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_socketpair",11,0,0,0;.stabs "socketpair",1,0,0,0
Index: trunk/src/emx/src/libsocket/aliases/soclose-alias.s
===================================================================
--- trunk/src/emx/src/libsocket/aliases/soclose-alias.s	(revision 886)
+++ trunk/src/emx/src/libsocket/aliases/soclose-alias.s	(revision 886)
@@ -0,0 +1,1 @@
+.stabs "_soclose",11,0,0,0;.stabs "soclose",1,0,0,0
Index: trunk/src/emx/src/libsocket/bind.c
===================================================================
--- trunk/src/emx/src/libsocket/bind.c	(revision 885)
+++ trunk/src/emx/src/libsocket/bind.c	(revision 886)
@@ -1,8 +1,51 @@
-/* bind.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
+/* $Id: $ */
+/** @file
+ *
+ * bind().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
-#include <emx/syscalls.h>
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
 
-int bind (int handle, struct sockaddr *addr, int addrlen)
+int bind(int socket, const struct sockaddr *addr, int addrlen)
 {
-  return __bind (handle, addr, addrlen);
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_bind(pFHSocket->iSocket, addr, addrlen);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
 }
+
Index: trunk/src/emx/src/libsocket/bsdselect.c
===================================================================
--- trunk/src/emx/src/libsocket/bsdselect.c	(revision 885)
+++ trunk/src/emx/src/libsocket/bsdselect.c	(revision 886)
@@ -1,82 +1,229 @@
-/* $Id$
- *
- * Wrapper for bsdselect() in BSD 4.4 mode.
- *
- * Copyright (c) 2003 InnoTek Systemberatung GmbH
- * Author: knut st. osmundsen <bird@anduin.net>
- *
- * All Rights Reserved
- *
- */
-#include <stdlib.h>
-#include <alloca.h>
-#include <sys/types.h>
-#include <sys/select.h>
-
-
-#define V5_FD_SETSIZE      64
-
-#pragma pack(4)
-typedef struct v5_fd_set {
-        u_short fd_count;               /* how many are SET? */
-        int     fd_array[V5_FD_SETSIZE];/* an array of SOCKETs */
-} v5_fd_set;
-#pragma pack()
-
-#define V5_FD_SET(fd, set) do { \
-    if (((v5_fd_set *)(set))->fd_count < V5_FD_SETSIZE) \
-        ((v5_fd_set *)(set))->fd_array[((v5_fd_set *)(set))->fd_count++]=fd;\
-} while(0)
-
-int _System v5_bsdselect(int, struct v5_fd_set *, struct v5_fd_set *, struct v5_fd_set *, struct timeval *);
-
-
-static inline void convert(int c, const struct fd_set *pFrom, struct v5_fd_set *pTo)
-{
-    int i = 0;
-
-    memset(pTo, 0, sizeof(struct v5_fd_set));
+/* $Id$ */
+/** @file
+ *
+ * bsdselect().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+
+/*******************************************************************************
+*   Defined Constants And Macros                                               *
+*******************************************************************************/
+#ifdef TCPV40HDRS
+
+#define MY_FD_SET(fd, set)      FD_SET(fd, set)
+#define MY_FD_ISSET(fd, set)    FD_ISSET(fd, set)
+#define MY_FD_ZERO(set, cb)     memset(set, 0, cb);
+#define my_fd_set               fd_set
+
+#else
+
+#define MY_FD_SET(fd, set)      V5_FD_SET(fd, set)
+#define	MY_FD_ISSET(fd, set)    V5_FD_ISSET(fd, set)
+#define MY_FD_ZERO(set, cb)     memset(set, 0, cb);
+#define my_fd_set               v5_fd_set
+
+#endif
+
+
+/**
+ * Calculate the size required for the converted set structure.
+ * @returns number of bytes.
+ * @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)
+{
+    int cbRet;
+#ifdef TCPV40HDRS
+    cbRet = (c + 7) / 8;
+#else
+    int i;
+    for (cbRet = sizeof(u_short), i = 0; i < c; i++)
+        if (FD_ISSET(i, pFrom))
+            cbRet++;
+#endif
+    if (cbRet < sizeof(struct my_fd_set))
+        return sizeof(struct my_fd_set);
+    return cbRet;
+}
+
+/** Converts the LIBC fd_set strcture pointed to by pFrom with it's LIBC socket handles,
+ * to the fd_set strcuture used by the OS/2 tcpip and the OS/2 socket handles.
+ * @returns 0 on success.
+ * @returns -1 on failure, both errnos set.
+ * @param   c       Number of file descriptors specified in the call.
+ * @param   cb      Size of pTo. (used for zeroing it)
+ * @param   pFrom   The select input fd_set structure.
+ * @param   pTo     The structure we present to OS/2 TCPIP.
+ *                  This will be initialized.
+ */
+static inline int convert(int c, int cb, const struct fd_set *pFrom, struct my_fd_set *pTo)
+{
+    int i;
+    MY_FD_ZERO(pTo, cb)
     for (i = 0; i < c; i++)
     {
         if (FD_ISSET(i, pFrom))
-            V5_FD_SET(i, pTo);
-    }
-    /** @todo: check for overflow and return appropriate error code... */
-}
-
+        {
+            PLIBCSOCKETFH   pFHSocket = __libsocket_FH(i);
+            if (!pFHSocket)
+                return -1;
+            MY_FD_SET(pFHSocket->iSocket, pTo);
+        }
+    }
+    return 0;
+}
 
 /**
- * This wrapper is required to work around a new ushort field someone
- * at IBM added to speed up the macros accessing the select structure.
- * @remark Only BSD 4.4 mode.
- */
-int _System bsdselect(int nfds, struct fd_set *readfds, struct fd_set *writefds,
-                      struct fd_set *excptfds, struct timeval *timeout)
-{
-    int     rc;
-    struct v5_fd_set *pv5_readfds  = NULL;
-    struct v5_fd_set *pv5_writefds = NULL;
-    struct v5_fd_set *pv5_excptfds = NULL;
-    int     cfds = nfds <= 0 ? FD_SETSIZE : nfds;
-
+ * Updates the pTo structure with the fds marked ready in pFrom.
+ *
+ * @param   c       Number of file descriptors specified in the call.
+ * @param   pFrom   The structure returned from OS/2 TCPIP select.
+ * @param   pTo     The structure passed in to select which have to
+ *                  be updated for the return.
+ */
+static inline void update(int c, const struct my_fd_set *pFrom, struct fd_set *pTo)
+{
+    int i;
+    for (i = 0; i < c; i++)
+    {
+        if (FD_ISSET(i, pTo))
+        {
+            PLIBCSOCKETFH   pFHSocket = __libsocket_FH(i);
+            if (pFHSocket)
+            {
+                if (!MY_FD_ISSET(pFHSocket->iSocket, pFrom))
+                    FD_CLR(i, pTo);
+            }
+        }
+    }
+}
+
+
+
+int bsdselect(int nfds, struct fd_set *readfds, struct fd_set *writefds, struct fd_set *exceptfds, struct timeval *tv)
+{
+#ifdef TCPV40HDRS
+    struct fd_set      *pRead;
+    struct fd_set      *pWrite;
+    struct fd_set      *pExcept;
+#else
+    struct v5_fd_set   *pRead;
+    struct v5_fd_set   *pWrite;
+    struct v5_fd_set   *pExcept;
+#endif
+    int                 cb;
+    int                 rc;
+
+    /*
+     * Allocate new bitmaps.
+     */
+    pRead = NULL;
     if (readfds)
     {
-        pv5_readfds = alloca(sizeof(struct v5_fd_set));
-        convert(cfds, readfds, pv5_readfds);
-    }
-
+        cb = calcsize(nfds, readfds);
+        pRead = alloca(cb);
+        if (!pRead)
+        {
+            __libsocket_setErrno(ENOMEM);
+            return -1;
+        }
+        if (convert(nfds, cb, readfds, pRead))
+            return -1;
+    }
+
+    pWrite = NULL;
     if (writefds)
     {
-        pv5_writefds = alloca(sizeof(struct v5_fd_set));
-        convert(cfds, writefds, pv5_writefds);
-    }
-
-    if (excptfds)
-    {
-        pv5_excptfds = alloca(sizeof(struct v5_fd_set));
-        convert(cfds, excptfds, pv5_excptfds);
-    }
-
-    rc = v5_bsdselect(nfds, pv5_readfds, pv5_writefds, pv5_excptfds, timeout);
+        cb = calcsize(nfds, writefds);
+        pWrite = alloca(cb);
+        if (!pWrite)
+        {
+            __libsocket_setErrno(ENOMEM);
+            return -1;
+        }
+        if (convert(nfds, cb, writefds, pWrite))
+            return -1;
+    }
+
+    pExcept = NULL;
+    if (exceptfds)
+    {
+        cb = calcsize(nfds, exceptfds);
+        pExcept = alloca(cb);
+        if (!pExcept)
+        {
+            __libsocket_setErrno(ENOMEM);
+            return -1;
+        }
+        if (convert(nfds, cb, exceptfds, pExcept))
+            return -1;
+    }
+
+    /*
+     * Do the call.
+     */
+    rc = __libsocket_bsdselect(nfds, pRead, pWrite, pExcept, tv);
+    if (rc < 0)
+    {
+        __libsocket_setLibcErrno();
+        return rc;
+    }
+
+    /*
+     * Timeout?
+     *  Just clear the bitmaps and return.
+     */
+    if (rc == 0)
+    {
+        cb = (nfds + 7) / 8;
+        if (readfds)
+            memset(readfds, 0, cb);
+        if (writefds)
+            memset(writefds, 0, cb);
+        if (exceptfds)
+            memset(exceptfds, 0, cb);
+        return 0;
+    }
+
+    /*
+     * Convert the bitmaps.
+     */
+    if (readfds)
+        update(nfds, pRead, readfds);
+    if (writefds)
+        update(nfds, pWrite, writefds);
+    if (exceptfds)
+        update(nfds, pExcept, exceptfds);
 
     return rc;
@@ -84,2 +231,3 @@
 
 
+
Index: trunk/src/emx/src/libsocket/connect.c
===================================================================
--- trunk/src/emx/src/libsocket/connect.c	(revision 885)
+++ trunk/src/emx/src/libsocket/connect.c	(revision 886)
@@ -1,8 +1,51 @@
-/* connect.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
+/* $Id: $ */
+/** @file
+ *
+ * connect().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
-#include <emx/syscalls.h>
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
 
-int connect (int handle, struct sockaddr *addr, int addrlen)
+int connect(int socket, const struct sockaddr *addr, int addrlen)
 {
-  return __connect (handle, addr, addrlen);
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_connect(pFHSocket->iSocket, addr, addrlen);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
 }
+
Index: trunk/src/emx/src/libsocket/getpeername.c
===================================================================
--- trunk/src/emx/src/libsocket/getpeername.c	(revision 886)
+++ trunk/src/emx/src/libsocket/getpeername.c	(revision 886)
@@ -0,0 +1,51 @@
+/* $Id$ */
+/** @file
+ *
+ * getpeername().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+int getpeername(int socket, struct sockaddr *addr, int *addrlen)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_getpeername(pFHSocket->iSocket, addr, addrlen);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
+}
+
Index: trunk/src/emx/src/libsocket/getsockname.c
===================================================================
--- trunk/src/emx/src/libsocket/getsockname.c	(revision 886)
+++ trunk/src/emx/src/libsocket/getsockname.c	(revision 886)
@@ -0,0 +1,52 @@
+/* $Id$ */
+/** @file
+ *
+ * getsockname().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+int getsockname(int socket, struct sockaddr *addr, int *addrlen)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_getsockname(pFHSocket->iSocket, addr, addrlen);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
+}
+
+
Index: trunk/src/emx/src/libsocket/getsockopt.c
===================================================================
--- trunk/src/emx/src/libsocket/getsockopt.c	(revision 886)
+++ trunk/src/emx/src/libsocket/getsockopt.c	(revision 886)
@@ -0,0 +1,52 @@
+/* $Id$ */
+/** @file
+ *
+ * getsockopt().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+int getsockopt(int socket, int level, int optname, void *optval, int *optlen)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_getsockopt(pFHSocket->iSocket, level, optname, optval, optlen);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
+}
+
+
Index: trunk/src/emx/src/libsocket/herror.c
===================================================================
--- trunk/src/emx/src/libsocket/herror.c	(revision 885)
+++ trunk/src/emx/src/libsocket/herror.c	(revision 886)
@@ -1,5 +1,9 @@
 /* herror.c (emx+gcc) -- Copyright (c) 1994 by Eberhard Mattes */
 
-#define TCPV40HDRS
+#ifndef TCPV40HDRS
+#error TCPV40HDRS only
+#endif
+
+#include "libc-alias.h"
 #include <stdio.h>
 #include <sys/types.h>
@@ -8,5 +12,5 @@
 extern int h_errno;
 
-void herror (const char *string)
+void TCPCALL herror(const char *string)
 {
   int e = h_errno;
Index: trunk/src/emx/src/libsocket/listen.c
===================================================================
--- trunk/src/emx/src/libsocket/listen.c	(revision 885)
+++ trunk/src/emx/src/libsocket/listen.c	(revision 886)
@@ -1,8 +1,52 @@
-/* listen.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
+/* $Id: $ */
+/** @file
+ *
+ * listen().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
-#include <emx/syscalls.h>
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
 
-int listen (int handle, int backlog)
+int listen(int socket, int backlog)
 {
-  return __listen (handle, backlog);
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_listen(pFHSocket->iSocket, backlog);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
 }
+
+
Index: trunk/src/emx/src/libsocket/os2_ioctl.c
===================================================================
--- trunk/src/emx/src/libsocket/os2_ioctl.c	(revision 886)
+++ trunk/src/emx/src/libsocket/os2_ioctl.c	(revision 886)
@@ -0,0 +1,61 @@
+/* $Id$ */
+/** @file
+ *
+ * ioctl().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <stdarg.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+#ifdef TCPV40HDRS
+int os2_ioctl(int socket, int request, char *arg, int len_arg)
+#else
+int os2_ioctl(int socket, unsigned long request, char *arg, int len_arg)
+#endif
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+#ifdef TCPV40HDRS
+        rc = __libsocket_os2_ioctl(pFHSocket->iSocket, __IOCLW(request), arg, len_arg);
+#else
+        rc = __libsocket_os2_ioctl(pFHSocket->iSocket, request, arg, len_arg);
+#endif
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+    return -1;
+}
+
+
+
Index: trunk/src/emx/src/libsocket/os2_select.c
===================================================================
--- trunk/src/emx/src/libsocket/os2_select.c	(revision 886)
+++ trunk/src/emx/src/libsocket/os2_select.c	(revision 886)
@@ -0,0 +1,76 @@
+/* $Id$ */
+/** @file
+ *
+ * os2_select().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+int os2_select(int *s, int nreads, int nwrites, int nexcepts, long timeout)
+{
+    int     cSockets;
+    int    *aSockets;
+    int     i;
+    int     rc;
+
+    /*
+     * Convert the sockets.
+     */
+    cSockets = nreads + nwrites + nexcepts;
+    aSockets = alloca(cSockets * sizeof(int));
+    if (aSockets)
+    {
+        __libsocket_setErrno(ENOMEM);
+        return -1;
+    }
+    for (i = 0; i < cSockets; i++)
+    {
+        PLIBCSOCKETFH   pFHSocket = __libsocket_FH(s[i]);
+        if (!pFHSocket)
+        {
+            __libsocket_setErrno(EBADF);
+            return -1;
+        }
+        aSockets[i] = pFHSocket->iSocket;
+    }
+
+    /*
+     * Do the call with new array.
+     */
+    rc = __libsocket_os2_select(aSockets, nreads, nwrites, nexcepts, timeout);
+    if (rc >= 0)
+        return rc;
+    __libsocket_setLibcErrno();
+    return -1;
+}
+
+
+
Index: trunk/src/emx/src/libsocket/recv.c
===================================================================
--- trunk/src/emx/src/libsocket/recv.c	(revision 885)
+++ trunk/src/emx/src/libsocket/recv.c	(revision 886)
@@ -1,8 +1,52 @@
-/* recv.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
+/* $Id: $ */
+/** @file
+ *
+ * recv().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
-#include <emx/syscalls.h>
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
 
-int recv (int handle, void *buf, int len, unsigned flags)
+int recv(int socket, void *buf, int len, int flags)
 {
-  return __recv (handle, buf, len, flags);
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_recv(pFHSocket->iSocket, buf, len, flags);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
 }
+
+
Index: trunk/src/emx/src/libsocket/recvfrom.c
===================================================================
--- trunk/src/emx/src/libsocket/recvfrom.c	(revision 885)
+++ trunk/src/emx/src/libsocket/recvfrom.c	(revision 886)
@@ -1,17 +1,52 @@
-/* recvfrom.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
+/* $Id: $ */
+/** @file
+ *
+ * recvfrom().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
-#include <emx/syscalls.h>
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
 
-int recvfrom (int handle, void *buf, int len, unsigned flags,
-              struct sockaddr *from, int *pfromlen)
+int recvfrom(int socket, void *buf, int len, int flags, struct sockaddr *from, int *fromlen)
 {
-  struct _recvfrom args;
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_recvfrom(pFHSocket->iSocket, buf, len, flags, from, fromlen);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
 
-  args.handle   = handle;
-  args.buf      = buf;
-  args.len      = len;
-  args.flags    = flags;
-  args.from     = from;
-  args.pfromlen = pfromlen;
-  return __recvfrom (&args);
+    return -1;
 }
+
+
Index: trunk/src/emx/src/libsocket/recvmsg.c
===================================================================
--- trunk/src/emx/src/libsocket/recvmsg.c	(revision 886)
+++ trunk/src/emx/src/libsocket/recvmsg.c	(revision 886)
@@ -0,0 +1,52 @@
+/* $Id$ */
+/** @file
+ *
+ * recvmsg().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+int recvmsg(int socket, struct msghdr *msg, int flags)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_recvmsg(pFHSocket->iSocket, msg, flags);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
+}
+
+
Index: trunk/src/emx/src/libsocket/send.c
===================================================================
--- trunk/src/emx/src/libsocket/send.c	(revision 885)
+++ trunk/src/emx/src/libsocket/send.c	(revision 886)
@@ -1,8 +1,52 @@
-/* send.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
+/* $Id: $ */
+/** @file
+ *
+ * send().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
-#include <emx/syscalls.h>
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
 
-int send (int handle, const void *buf, int len, unsigned flags)
+int send(int socket, const void *buf, int len, int flags)
 {
-  return __send (handle, buf, len, flags);
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_send(pFHSocket->iSocket, buf, len, flags);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
 }
+
+
Index: trunk/src/emx/src/libsocket/sendmsg.c
===================================================================
--- trunk/src/emx/src/libsocket/sendmsg.c	(revision 886)
+++ trunk/src/emx/src/libsocket/sendmsg.c	(revision 886)
@@ -0,0 +1,52 @@
+/* $Id$ */
+/** @file
+ *
+ * sendmsg().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+int sendmsg(int socket, const struct msghdr *msg, int flags)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_sendmsg(pFHSocket->iSocket, msg, flags);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
+}
+
+
Index: trunk/src/emx/src/libsocket/sendto.c
===================================================================
--- trunk/src/emx/src/libsocket/sendto.c	(revision 885)
+++ trunk/src/emx/src/libsocket/sendto.c	(revision 886)
@@ -1,17 +1,51 @@
-/* sendto.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
+/* $Id: $ */
+/** @file
+ *
+ * sendto().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
-#include <emx/syscalls.h>
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
 
-int sendto (int handle, const void *buf, int len, unsigned flags,
-            const struct sockaddr *to, int tolen)
+int sendto(int socket, const void *buf, int len, int flags, const struct sockaddr *to, int tolen)
 {
-  struct _sendto args;
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int     rc;
+        rc = __libsocket_sendto(pFHSocket->iSocket, buf, len, flags, to, tolen);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
 
-  args.handle = handle;
-  args.buf    = buf;
-  args.len    = len;
-  args.flags  = flags;
-  args.to     = to;
-  args.tolen  = tolen;
-  return __sendto (&args);
+    return -1;
 }
+
Index: trunk/src/emx/src/libsocket/setsockopt.c
===================================================================
--- trunk/src/emx/src/libsocket/setsockopt.c	(revision 886)
+++ trunk/src/emx/src/libsocket/setsockopt.c	(revision 886)
@@ -0,0 +1,51 @@
+/* $Id$ */
+/** @file
+ *
+ * setsockopt().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+int setsockopt(int socket, int level, int optname, const void *optval, int optlen)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_setsockopt(pFHSocket->iSocket, level, optname, optval, optlen);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
+}
+
Index: trunk/src/emx/src/libsocket/shutdown.c
===================================================================
--- trunk/src/emx/src/libsocket/shutdown.c	(revision 885)
+++ trunk/src/emx/src/libsocket/shutdown.c	(revision 886)
@@ -1,8 +1,52 @@
-/* shutdown.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
+/* $Id: $ */
+/** @file
+ *
+ * shutdown().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
-#include <emx/syscalls.h>
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
 
-int shutdown (int handle, int how)
+int shutdown(int socket, int how)
 {
-  return __shutdown (handle, how);
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_shutdown(pFHSocket->iSocket, how);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
 }
+
+
Index: trunk/src/emx/src/libsocket/so_cancel.c
===================================================================
--- trunk/src/emx/src/libsocket/so_cancel.c	(revision 886)
+++ trunk/src/emx/src/libsocket/so_cancel.c	(revision 886)
@@ -0,0 +1,52 @@
+/* $Id$ */
+/** @file
+ *
+ * so_cancel().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+int so_cancel(int socket)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_so_cancel(pFHSocket->iSocket);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
+}
+
+
Index: trunk/src/emx/src/libsocket/so_ioctl.c
===================================================================
--- trunk/src/emx/src/libsocket/so_ioctl.c	(revision 886)
+++ trunk/src/emx/src/libsocket/so_ioctl.c	(revision 886)
@@ -0,0 +1,54 @@
+/* $Id$ */
+/** @file
+ *
+ * ioctl().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <stdarg.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+int so_ioctl(int socket, unsigned long request, ...)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        va_list args;
+        int     iArg;
+        va_start(args, request);
+        iArg = va_arg(args, int);
+        va_end(args);
+        return ioctl(socket, request, iArg);
+    }
+    return -1;
+}
+
+
+
Index: trunk/src/emx/src/libsocket/so_readv.c
===================================================================
--- trunk/src/emx/src/libsocket/so_readv.c	(revision 886)
+++ trunk/src/emx/src/libsocket/so_readv.c	(revision 886)
@@ -0,0 +1,52 @@
+/* $Id$ */
+/** @file
+ *
+ * so_readv().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+ssize_t so_readv(int socket, struct iovec *iov, int iovcnt)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        ssize_t rc;
+        rc = __libsocket_so_readv(pFHSocket->iSocket, iov, iovcnt);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
+}
+
+
Index: trunk/src/emx/src/libsocket/so_writev.c
===================================================================
--- trunk/src/emx/src/libsocket/so_writev.c	(revision 886)
+++ trunk/src/emx/src/libsocket/so_writev.c	(revision 886)
@@ -0,0 +1,52 @@
+/* $Id$ */
+/** @file
+ *
+ * so_writev().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+ssize_t so_writev(int socket, struct iovec *iov, int iovcnt)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        ssize_t rc;
+        rc = __libsocket_so_writev(pFHSocket->iSocket, iov, iovcnt);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
+}
+
+
Index: trunk/src/emx/src/libsocket/soabort.c
===================================================================
--- trunk/src/emx/src/libsocket/soabort.c	(revision 886)
+++ trunk/src/emx/src/libsocket/soabort.c	(revision 886)
@@ -0,0 +1,55 @@
+/* $Id$ */
+/** @file
+ *
+ * soabort().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+/* not declared anywhere */
+int	TCPCALL soabort(int fh);
+
+int soabort(int socket)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        int rc;
+        rc = __libsocket_soabort(pFHSocket->iSocket);
+        if (rc >= 0)
+            return rc;
+        __libsocket_setLibcErrno();
+    }
+
+    return -1;
+}
+
+
Index: trunk/src/emx/src/libsocket/sock_errno.c
===================================================================
--- trunk/src/emx/src/libsocket/sock_errno.c	(revision 885)
+++ trunk/src/emx/src/libsocket/sock_errno.c	(revision 886)
@@ -10,6 +10,14 @@
  */
 
-#include <sys/types.h>
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
 #include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
 
 /**
@@ -24,6 +32,6 @@
     /* get the OS/2 error. */
     int err = os2_sock_errno();
-    if (err > 10000 && err < 11000)
-        err -= 10000;
+    if (err > EOS2_TCPIP_OFFSET && err < EOS2_TCPIP_OFFSET + 1000)
+        err -= EOS2_TCPIP_OFFSET;
     return err;
 }
Index: trunk/src/emx/src/libsocket/socket.c
===================================================================
--- trunk/src/emx/src/libsocket/socket.c	(revision 885)
+++ trunk/src/emx/src/libsocket/socket.c	(revision 886)
@@ -1,23 +1,63 @@
-/* socket.c (emx+gcc) -- Copyright (c) 1994-1996 by Eberhard Mattes */
+/* $Id: $ */
+/** @file
+ *
+ * socket().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
 #include <sys/fcntl.h>
-#include <errno.h>
 #include <emx/io.h>
-#include <emx/syscalls.h>
+#include "socket.h"
 
-int socket (int domain, int type, int protocol)
+
+int socket(int af, int type, int protocol)
 {
-  int s, *pflags;
+    int s;
 
-  s = __socket (domain, type, protocol);
-  if (s < 0)
+    s = __libsocket_socket(af, type, protocol);
+    if (s >= 0)
+    {
+        int             rc;
+        int             fh;
+        PLIBCSOCKETFH   pFH;
+        rc = __libc_FHAllocate(-1, O_RDWR | F_SOCKET, sizeof(LIBCSOCKETFH), &__libsocket_gSocketOps, (PLIBCFH*)&pFH, &fh);
+        if (!rc)
+        {
+            pFH->iSocket = s;
+            return fh;
+        }
+        else
+            __libsocket_soclose(s);
+        __libsocket_setSocketErrno();
+    }
+    else
+        __libsocket_setLibcErrno();
+
     return -1;
-  if ((pflags = _fd_init (s)) == NULL)
-    {
-      __close (s);
-      errno = EMFILE;
-      return -1;
-    }
-  *pflags = O_RDWR | F_SOCKET;
-  return s;
 }
+
Index: trunk/src/emx/src/libsocket/socket.h
===================================================================
--- trunk/src/emx/src/libsocket/socket.h	(revision 886)
+++ trunk/src/emx/src/libsocket/socket.h	(revision 886)
@@ -0,0 +1,232 @@
+/* $Id$ */
+/** @file
+ *
+ * Interal libsocket stuff.
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#ifndef __socket_h__
+#define __socket_h__
+
+
+/** @group libsocket    libsocket
+ * @{
+ */
+
+/*******************************************************************************
+*   Defined Constants And Macros                                               *
+*******************************************************************************/
+/** The offset the OS/2 TCP/IP errno values are skewed compared to the
+ * LIBC errno values. */
+#define EOS2_TCPIP_OFFSET   10000
+
+/** Get the low word of the ioctl request number.
+ * Used to support ioctl request numbers from old and new _IOC macros.
+ */
+#define __IOCLW(a) ((unsigned short)(a))
+
+#ifndef TCPV40HDRS
+
+#define V5_FD_SETSIZE      64
+#define V5_FD_SET(fd, set) do { \
+   /* if (((v5_fd_set *)(set))->fd_count < V5_FD_SETSIZE) - calcsize fixes this */ \
+        ((v5_fd_set *)(set))->fd_array[((v5_fd_set *)(set))->fd_count++]=fd;\
+} while(0)
+#define V5_FD_ISSET(fd, set)    v5_isset(fd, set)
+
+#endif
+
+
+/*******************************************************************************
+*   Structures and Typedefs                                                    *
+*******************************************************************************/
+/** Socket handle.*/
+typedef struct __libc_SocketHandle
+{
+    /** the common fh core. */
+    LIBCFH      core;
+
+    /** OS/2 socket number. */
+    int         iSocket;
+} LIBCSOCKETFH, *PLIBCSOCKETFH;
+
+
+#ifndef TCPV40HDRS
+
+#pragma pack(4)
+/** OS/2 oddities from the BSD 4.4 stack. */
+typedef struct v5_fd_set {
+        u_short fd_count;               /* how many are SET? */
+        int     fd_array[V5_FD_SETSIZE];/* an array of SOCKETs */
+} v5_fd_set;
+#pragma pack()
+
+/** internal helper */
+static inline int v5_isset(int fd, const struct v5_fd_set *set)
+{
+    const int  *pfd = &set->fd_array[0];
+    int         c   = set->fd_count;
+    while (c > 0)
+    {
+        if (*pfd == fd)
+            return 1;
+        pfd++;
+        c--;
+    }
+    return 0;
+}
+
+#endif
+
+
+/*******************************************************************************
+*   Global Variables                                                           *
+*******************************************************************************/
+extern LIBCFHOPS __libsocket_gSocketOps;
+
+
+/*******************************************************************************
+*   Internal Functions                                                         *
+*******************************************************************************/
+/** @defgroup libsocket_renamed     Renamed Imports.
+ * @{ */
+int     TCPCALL __libsocket_accept(int, struct sockaddr *, int *);
+int     TCPCALL __libsocket_bind(int, __const__ struct sockaddr *, int);
+int     TCPCALL __libsocket_connect(int, __const__ struct sockaddr *, int);
+int     TCPCALL __libsocket_getpeername(int, struct sockaddr *, int *);
+int     TCPCALL __libsocket_getsockname(int, struct sockaddr *, int *);
+int     TCPCALL __libsocket_getsockopt(int, int, int, void *, int *);
+int     TCPCALL __libsocket_ioctl(int, int, char *);
+int     TCPCALL __libsocket_listen(int, int);
+int     TCPCALL __libsocket_os2_ioctl(int, unsigned long, char *, int);
+int     TCPCALL __libsocket_os2_select(int *, int, int, int, long);
+int     TCPCALL __libsocket_recv(int, void *, int, int);
+int     TCPCALL __libsocket_recv(int, void *, int, int);
+int     TCPCALL __libsocket_recvfrom(int, void *, int, int, struct sockaddr *, int *);
+int     TCPCALL __libsocket_recvmsg(int, struct msghdr *, int);
+int     TCPCALL __libsocket_send(int, __const__ void *, int, int);
+int     TCPCALL __libsocket_send(int, const void *, int, int);
+int     TCPCALL __libsocket_sendmsg(int, __const__ struct msghdr *, int);
+int     TCPCALL __libsocket_sendto(int, __const__ void *, int, int, __const__ struct sockaddr *, int);
+int     TCPCALL __libsocket_setsockopt(int, int, int, __const__ void *, int);
+int     TCPCALL __libsocket_shutdown(int, int);
+int     TCPCALL __libsocket_sock_errno(void);
+int     TCPCALL __libsocket_socket(int, int, int);
+int     TCPCALL __libsocket_socket(int, int, int);
+int     TCPCALL __libsocket_socketpair(int, int, int, int *);
+int     TCPCALL __libsocket_soclose(int);
+int     TCPCALL __libsocket_tcpipv4_ioctl(int, unsigned long, char *, int);
+int     TCPCALL __libsocket_accept_and_recv(long, long*, struct sockaddr *, long*, struct sockaddr*, long*, caddr_t, size_t);
+void    TCPCALL __libsocket_addsockettolist(int);
+int     TCPCALL __libsocket_removesocketfromlist(int);
+ssize_t TCPCALL __libsocket_so_readv(int, struct iovec *, int);
+ssize_t TCPCALL __libsocket_so_writev(int, struct iovec *, int);
+int     TCPCALL __libsocket_so_cancel(int);
+int     TCPCALL __libsocket_soabort(int);
+int     TCPCALL __libsocket_Raccept(int, struct sockaddr *, int *);
+struct sockaddr_in;
+int     TCPCALL __libsocket_tcpip4_Rbind(int, struct sockaddr_in *, int, struct sockaddr_in *);
+int     TCPCALL __libsocket_Rbind(int, struct sockaddr *, int, struct sockaddr *);
+int     TCPCALL __libsocket_Rconnect(int, const struct sockaddr *, int);
+int     TCPCALL __libsocket_Rgetsockname(int, struct sockaddr *, int *);
+int     TCPCALL __libsocket_Rlisten(int, int);
+#ifndef TCPV40HDRS
+ssize_t TCPCALL __libsocket_send_file(int *, struct sf_parms *, int );
+#endif
+char *  TCPCALL __libsocket_sock_strerror(int);
+int     TCPCALL __libsocket_pair(int af, int type, int flags, int *osfd);
+#ifdef TCPV40HDRS
+int     TCPCALL __libsocket_bsdselect(int, fd_set *, fd_set *, fd_set *, struct timeval *);
+#else
+int     TCPCALL __libsocket_bsdselect(int, v5_fd_set *, v5_fd_set *, v5_fd_set *, struct timeval *);
+#endif
+void    TCPCALL __libsocket_set_errno(int);
+/** @} */
+
+
+/** @defgroup libsocket_internal    Internal helpers.
+ * @{ */
+/**
+ * Sets the LIBC and socket errno variables to a given error number.
+ */
+static inline void  __libsocket_setErrno(int err)
+{
+    errno = err;
+    __libsocket_set_errno(err + EOS2_TCPIP_OFFSET);
+}
+
+/**
+ * Updates the LIBC errno with the latest socket error.
+ */
+static inline void  __libsocket_setLibcErrno(void)
+{
+    int err = __libsocket_sock_errno();
+    if (err >= EOS2_TCPIP_OFFSET && err < EOS2_TCPIP_OFFSET + 1000)
+        errno = err - EOS2_TCPIP_OFFSET;
+}
+
+/**
+ * Updates the socket errno with the latest LIBC error.
+ */
+static inline void  __libsocket_setSocketErrno(void)
+{
+    __libsocket_set_errno(errno + EOS2_TCPIP_OFFSET);
+}
+
+/**
+ * Gets the socket errno translating it to a LIBC errno.
+ */
+static inline int   __libsocket_getSocketErrno(void)
+{
+    int err = __libsocket_sock_errno();
+    if (err >= EOS2_TCPIP_OFFSET && err < EOS2_TCPIP_OFFSET + 1000)
+        return err - EOS2_TCPIP_OFFSET;
+    return EDOOFUS;
+}
+
+
+/**
+ * Retrieve the socket handle structure for a given handle.
+ * @returns Pointer to socket handle structure on success.
+ * @returns NULL on failure with errno set to the appropriate value.
+ * @param   socket  Socket handle number.
+ */
+static inline PLIBCSOCKETFH   __libsocket_FH(int socket)
+{
+    PLIBCSOCKETFH   pFHSocket = (PLIBCSOCKETFH)__libc_FH(socket);
+    if (pFHSocket)
+    {
+        if ((pFHSocket->core.fFlags & F_TYPEMASK) == F_SOCKET)
+            return pFHSocket;
+        __libsocket_setErrno(ENOSYS);
+    }
+    else
+        __libsocket_setErrno(EBADF);
+    return NULL;
+}
+
+/** @} */
+
+
+/** @} */
+
+#endif
Index: trunk/src/emx/src/libsocket/socket.imp
===================================================================
--- trunk/src/emx/src/libsocket/socket.imp	(revision 885)
+++ trunk/src/emx/src/libsocket/socket.imp	(revision 886)
@@ -5,54 +5,31 @@
 ;
 ; -------- tcpip32.lib --------
-accept                  tcpip32    1 ?
-_accept                 tcpip32    1 ?
-bind                    tcpip32    2 ?
-_bind                   tcpip32    2 ?
-connect                 tcpip32    3 ?
-_connect                tcpip32    3 ?
+__libsocket_accept      tcpip32    1 ?
+__libsocket_bind        tcpip32    2 ?
+__libsocket_connect     tcpip32    3 ?
 gethostid               tcpip32    4 ?
 _gethostid              tcpip32    4 ?
-getpeername             tcpip32    5 ?
-_getpeername            tcpip32    5 ?
-getsockname             tcpip32    6 ?
-_getsockname            tcpip32    6 ?
-getsockopt              tcpip32    7 ?
-_getsockopt             tcpip32    7 ?
-so_ioctl                tcpip32    8 ?
-_so_ioctl               tcpip32    8 ?
-listen                  tcpip32    9 ?
-_listen                 tcpip32    9 ?
-recv                    tcpip32   10 ?
-_recv                   tcpip32   10 ?
-recvfrom                tcpip32   11 ?
-_recvfrom               tcpip32   11 ?
-os2_select              tcpip32   12 ?
-_os2_select             tcpip32   12 ?
-send                    tcpip32   13 ?
-_send                   tcpip32   13 ?
-sendto                  tcpip32   14 ?
-_sendto                 tcpip32   14 ?
-setsockopt              tcpip32   15 ?
-_setsockopt             tcpip32   15 ?
-socket                  tcpip32   16 ?
-_socket                 tcpip32   16 ?
-soclose                 tcpip32   17 ?
-_soclose                tcpip32   17 ?
-so_cancel               tcpip32   18 ?
-_so_cancel              tcpip32   18 ?
-soabort                 tcpip32   19 ?
-_soabort                tcpip32   19 ?
+__libsocket_getpeername tcpip32    5 ?
+__libsocket_getsockname tcpip32    6 ?
+__libsocket_getsockopt  tcpip32    7 ?
+__libsocket_ioctl       tcpip32    8 ?
+__libsocket_listen      tcpip32    9 ?
+__libsocket_recv        tcpip32   10 ?
+__libsocket_recvfrom    tcpip32   11 ?
+__libsocket_os2_select  tcpip32   12 ?
+__libsocket_send        tcpip32   13 ?
+__libsocket_sendto      tcpip32   14 ?
+__libsocket_setsockopt  tcpip32   15 ?
+__libsocket_socket      tcpip32   16 ?
+__libsocket_soclose     tcpip32   17 ?
+__libsocket_so_cancel   tcpip32   18 ?
+__libsocket_soabort     tcpip32   19 ?
 os2_sock_errno          tcpip32   20 ?
-_os2_sock_errno        tcpip32   20 ?
-recvmsg                 tcpip32   21 ?
-_recvmsg                tcpip32   21 ?
-sendmsg                 tcpip32   22 ?
-_sendmsg                tcpip32   22 ?
-so_readv                tcpip32   23 ?
-_so_readv               tcpip32   23 ?
-so_writev               tcpip32   24 ?
-_so_writev              tcpip32   24 ?
-shutdown                tcpip32   25 ?
-_shutdown               tcpip32   25 ?
+_os2_sock_errno         tcpip32   20 ?
+__libsocket_recvmsg     tcpip32   21 ?
+__libsocket_sendmsg     tcpip32   22 ?
+__libsocket_so_readv    tcpip32   23 ?
+__libsocket_so_writev   tcpip32   24 ?
+__libsocket_shutdown    tcpip32   25 ?
 sock_init               tcpip32   26 ?
 _sock_init              tcpip32   26 ?
@@ -67,7 +44,6 @@
 getinetversion          tcpip32   31 ?
 _getinetversion         tcpip32   31 ?
-v5_bsdselect            tcpip32   32 ?
-set_errno               tcpip32   35 ?
-_set_errno              tcpip32   35 ?
+__libsocket_bsdselect   tcpip32   32 ?
+__libsocket_set_errno   tcpip32   35 ?
 winsockcleanupsockets   tcpip32   38 ?
 _winsockcleanupsockets  tcpip32   38 ?
@@ -174,6 +150,5 @@
 Rgethostbyname          tcpip32  161 ?
 _Rgethostbyname         tcpip32  161 ?
-os2_ioctl               tcpip32  200 ?
-_os2_ioctl              tcpip32  200 ?
+__libsocket_os2_ioctl   tcpip32  200 ?
 __TCPFDIsSet            tcpip32  201 ?
 ___TCPFDIsSet           tcpip32  201 ?
@@ -282,6 +257,5 @@
 inet_nsap_ntoa          tcpip32  253 ?
 _inet_nsap_ntoa         tcpip32  253 ?
-socketpair              tcpip32  254 ?
-_socketpair             tcpip32  254 ?
+__libsocket_socketpair  tcpip32  254 ?
 sock_strerror           tcpip32  255 ?
 _sock_strerror          tcpip32  255 ?
Index: trunk/src/emx/src/libsocket/socket.smak
===================================================================
--- trunk/src/emx/src/libsocket/socket.smak	(revision 885)
+++ trunk/src/emx/src/libsocket/socket.smak	(revision 886)
@@ -3,12 +3,15 @@
 .MODULE	:= libsocket
 .MDESC	:= The socket library
-_MDIRS  := $.aout/tcpipv4 $.aout-prof/tcpipv4 $.aout/dep-tcpipv4 $.aout-prof/dep-tcpipv4 
-.MDEP   := emxomf emximp $(_MDIRS)
-TARGDIRS += $(_MDIRS)
+#_MDIRS  := $.aout/tcpipv4 $.aout-prof/tcpipv4 $.aout/dep-tcpipv4 $.aout-prof/dep-tcpipv4
+.MDEP   := emxomf emximp
+#$(_MDIRS)
+#TARGDIRS += $(_MDIRS)
 
 .TARGET	:= libsocket_p.a
 .TKIND	:= aout prof
-.TSRC   := $(wildcard src/lib/socket/386/*.s)
-.TSRC   += src/lib/socket/sock_errno.c src/lib/socket/bsdselect.c
+.TSRC   := $(wildcard src/lib/socket/386/*.s) \
+           $(filter-out src/lib/socket/aliases/herror%, $(wildcard src/lib/socket/aliases/*.s)) \
+           $(filter-out src/lib/socket/herror%, $(wildcard src/lib/socket/*.c))
+.TCF    := -I$.
 .IMPS   := src/lib/socket/socket.imp
 .INSDIR	:= lib/
@@ -18,32 +21,22 @@
 .TARGET	:= libsocket.a
 .TKIND	:= aout
-.TSRC   := $(wildcard src/lib/socket/386/*.s)
-.TSRC   += src/lib/socket/sock_errno.c src/lib/socket/bsdselect.c
-.IMPS   := src/lib/socket/socket.imp
-.INSDIR	:= lib/
 .TKEEP	:= 1
 include mkimplib.smak
 
 	
-.TARGET	:= tcpipv4/libsocket_p.a
+.TARGET	:= libsocket_p.a
 .TKIND	:= aout prof
-.TSRC   := $(wildcard src/lib/socket/386/*.s)
-.TSRC	+= src/lib/socket/herror.c src/lib/socket/sock_errno.c src/lib/socket/socketpair.c
-.TSRC	+= src/lib/socket/socketpair-alias.s
+.TKVAR	:= tcpipv4
+.TSRC   := $(wildcard src/lib/socket/386/*.s) $(wildcard src/lib/socket/aliases/*.s) \
+           $(wildcard src/lib/socket/*.c)
+.TCF    := -I$. -DTCPV40HDRS
 .IMPS   := src/lib/socket/socket_tcpipv4.imp
-.INSDIR	:= lib/
+.INSDIR	:= lib/tcpipv4
 .TKEEP	:= 1
 include mkimplib.smak
 
-.TARGET	:= tcpipv4/libsocket.a
+.TARGET	:= libsocket.a
 .TKIND	:= aout
-.TSRC   := $(wildcard src/lib/socket/386/*.s)
-.TSRC	+= src/lib/socket/herror.c src/lib/socket/sock_errno.c src/lib/socket/socketpair.c
-.TSRC	+= src/lib/socket/socketpair-alias.s
-.IMPS   := src/lib/socket/socket_tcpipv4.imp
-.TKEEP	:= 1
 include mkimplib.smak
-
-#$.omf/tcpipv4 omf-prof/tcpipv4
 
 
Index: trunk/src/emx/src/libsocket/socket_tcpipv4.imp
===================================================================
--- trunk/src/emx/src/libsocket/socket_tcpipv4.imp	(revision 885)
+++ trunk/src/emx/src/libsocket/socket_tcpipv4.imp	(revision 886)
@@ -5,54 +5,31 @@
 ;
 ; -------- so32dll.lib --------
-accept                  so32dll    1 ?
-_accept                 so32dll    1 ?
-bind                    so32dll    2 ?
-_bind                   so32dll    2 ?
-connect                 so32dll    3 ?
-_connect                so32dll    3 ?
+__libsocket_accept      so32dll    1 ?
+__libsocket_bind        so32dll    2 ?
+__libsocket_connect     so32dll    3 ?
 gethostid               so32dll    4 ?
 _gethostid              so32dll    4 ?
-getpeername             so32dll    5 ?
-_getpeername            so32dll    5 ?
-getsockname             so32dll    6 ?
-_getsockname            so32dll    6 ?
-getsockopt              so32dll    7 ?
-_getsockopt             so32dll    7 ?
-os2_ioctl               so32dll    8 ?
-_os2_ioctl              so32dll    8 ?
-listen                  so32dll    9 ?
-_listen                 so32dll    9 ?
-recv                    so32dll   10 ?
-_recv                   so32dll   10 ?
-recvfrom                so32dll   11 ?
-_recvfrom               so32dll   11 ?
-os2_select              so32dll   12 ?
-_os2_select             so32dll   12 ?
-send                    so32dll   13 ?
-_send                   so32dll   13 ?
-sendto                  so32dll   14 ?
-_sendto                 so32dll   14 ?
-setsockopt              so32dll   15 ?
-_setsockopt             so32dll   15 ?
-socket                  so32dll   16 ?
-_socket                 so32dll   16 ?
-soclose                 so32dll   17 ?
-_soclose                so32dll   17 ?
-so_cancel               so32dll   18 ?
-_so_cancel              so32dll   18 ?
-soabort                 so32dll   19 ?
-_soabort                so32dll   19 ?
+__libsocket_getpeername so32dll    5 ?
+__libsocket_getsockname so32dll    6 ?
+__libsocket_getsockopt  so32dll    7 ?
+__libsocket_os2_ioctl   so32dll    8 ?
+__libsocket_listen      so32dll    9 ?
+__libsocket_recv        so32dll   10 ?
+__libsocket_recvfrom    so32dll   11 ?
+__libsocket_os2_select  so32dll   12 ?
+__libsocket_send        so32dll   13 ?
+__libsocket_sendto      so32dll   14 ?
+__libsocket_setsockopt  so32dll   15 ?
+__libsocket_socket      so32dll   16 ?
+__libsocket_soclose     so32dll   17 ?
+__libsocket_so_cancel   so32dll   18 ?
+__libsocket_soabort     so32dll   19 ?
 os2_sock_errno          so32dll   20 ?
 _os2_sock_errno         so32dll   20 ?
-recvmsg                 so32dll   21 ?
-_recvmsg                so32dll   21 ?
-sendmsg                 so32dll   22 ?
-_sendmsg                so32dll   22 ?
-so_readv                so32dll   23 ?
-_so_readv               so32dll   23 ?
-so_writev               so32dll   24 ?
-_so_writev              so32dll   24 ?
-shutdown                so32dll   25 ?
-_shutdown               so32dll   25 ?
+__libsocket_recvmsg     so32dll   21 ?
+__libsocket_sendmsg     so32dll   22 ?
+__libsocket_so_readv    so32dll   23 ?
+__libsocket_so_writev   so32dll   24 ?
+__libsocket_shutdown    so32dll   25 ?
 sock_init               so32dll   26 ?
 _sock_init              so32dll   26 ?
@@ -65,8 +42,6 @@
 getinetversion          so32dll   31 ?
 _getinetversion         so32dll   31 ?
-bsdselect               so32dll   32 ?
-_bsdselect              so32dll   32 ?
-set_errno               so32dll   35 ?
-_set_errno              so32dll   35 ?
+__libsocket_bsdselect   so32dll   32 ?
+__libsocket_set_errno   so32dll   35 ?
 getsocketfromlist       so32dll   39 ?
 _getsocketfromlist      so32dll   39 ?
Index: trunk/src/emx/src/libsocket/socketops.c
===================================================================
--- trunk/src/emx/src/libsocket/socketops.c	(revision 886)
+++ trunk/src/emx/src/libsocket/socketops.c	(revision 886)
@@ -0,0 +1,533 @@
+/* $Id$ */
+/** @file
+ *
+ * socket handle operations.
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#define INCL_TCPIP_ALLIOCTLS
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/sockio.h>
+#include <net/if.h>
+#include <net/if_arp.h>
+#include <netinet/in_systm.h>
+#include <netinet/in.h>
+#include <netinet/if_ether.h>
+#include <netinet/ip.h>
+#include <netinet/ip_var.h>
+#include <netinet/ip_icmp.h>
+#include <netinet/tcp.h>
+#include <netinet/tcp_var.h>
+#include <netinet/udp.h>
+#include <netinet/udp_var.h>
+#include <net/route.h>
+
+#include <sys/fcntl.h>
+#include <sys/filio.h>
+#include <emx/io.h>
+#include "socket.h"
+
+
+/*******************************************************************************
+*   Structures and Typedefs                                                    *
+*******************************************************************************/
+#ifdef TCPV40HDRS
+/* not included in the TCPV40HDRS - this is from the BSD4.4 stack headers
+                                    and doesn't have to right!!! */
+struct arpreq_tr {
+        struct  sockaddr arp_pa;                /* protocol address */
+        struct  sockaddr arp_ha;                /* hardware address */
+        long_int arp_flags;                     /* flags */
+        u_short arp_rcf;                        /* token ring routing control field */
+        u_short arp_rseg[8];                    /* token ring routing segments */
+};
+
+#define MAX_IN_MULTI      16*IP_MAX_MEMBERSHIPS      /* 320 max per os2 */
+struct  addrreq  {                              /* get multicast addresses */
+        char    ifr_name[IFNAMSIZ];
+        struct  sockaddr ifr_addrs;
+        u_long  maddr[MAX_IN_MULTI];
+};
+
+#pragma pack(1)
+struct  oarptab {
+        struct  in_addr at_iaddr;       /* internet address */
+        u_char  at_enaddr[6];           /* ethernet address */
+        u_char  at_timer;               /* minutes since last reference */
+        u_char  at_flags;               /* flags */
+#ifdef KERNEL
+        struct  mbuf *at_hold;          /* last packet until resolved/timeout */
+#else
+        void * at_hold;
+#endif
+        u_short at_rcf;                 /* token ring routing control field */
+        u_short at_rseg[8];             /* token ring routing segments */
+        u_long  at_millisec;            /* TOD milliseconds of last update */
+        u_short at_interface;           /* interface index */
+};
+#pragma pack()
+
+#pragma pack(1)
+struct  statatreq {
+        u_long addr;
+        short interface;
+        u_long mask;
+        u_long broadcast;
+};
+#pragma pack()
+
+#endif
+
+/*******************************************************************************
+*   Internal Functions                                                         *
+*******************************************************************************/
+static int __libsocket_ops_Close(PLIBCFH pFH, int fh);
+static int __libsocket_ops_Read(PLIBCFH pFH, int fh, void *pvBuf, size_t cbRead, size_t *pcbRead);
+static int __libsocket_ops_Write(PLIBCFH pFH, int fh, const void *pvBuf, size_t cbWrite, size_t *pcbWritten);
+static int __libsocket_ops_Duplicate(PLIBCFH pFH, int fh, int *pfhNew);
+static int __libsocket_ops_FileControl(PLIBCFH pFH, int fh, int iIOControl, int iArg, int *prc);
+static int __libsocket_ops_IOControl(PLIBCFH pFH, int fh, int iIOControl, int iArg, int *prc);
+
+
+/*******************************************************************************
+*   Global Variables                                                           *
+*******************************************************************************/
+/** Operations on sockets. */
+LIBCFHOPS __libsocket_gSocketOps =
+{
+    enmFH_Socket, /* Handle type. */
+    __libsocket_ops_Close,
+    __libsocket_ops_Read,
+    __libsocket_ops_Write,
+    __libsocket_ops_Duplicate,
+    __libsocket_ops_FileControl,
+    __libsocket_ops_IOControl
+};
+
+
+/** Close operation.
+ * @returns 0 on success.
+ * @returns OS/2 error code or negated errno on failure.
+ * @param   pFH         Pointer to the handle structure to operate on.
+ * @param   fh          It's associated filehandle.
+ */
+static int __libsocket_ops_Close(PLIBCFH pFH, int fh)
+{
+    PLIBCSOCKETFH   pSocketFH = (PLIBCSOCKETFH)pFH;
+    int             rc;
+    rc = __libsocket_soclose(pSocketFH->iSocket);
+    if (rc)
+        return -__libsocket_getSocketErrno();
+    return 0;
+}
+
+
+/** Read operation.
+ * @returns 0 on success.
+ * @returns OS/2 error code or negated errno on failure.
+ * @param   pFH         Pointer to the handle structure to operate on.
+ * @param   fh          It's associated filehandle.
+ * @param   pvBuf       Pointer to the buffer to read into.
+ * @param   cbRead      Number of bytes to read.
+ * @param   pcbRead     Where to store the count of bytes actually read.
+ */
+static int __libsocket_ops_Read(PLIBCFH pFH, int fh, void *pvBuf, size_t cbRead, size_t *pcbRead)
+{
+    PLIBCSOCKETFH   pSocketFH = (PLIBCSOCKETFH)pFH;
+    int             rc;
+
+    rc = __libsocket_recv(pSocketFH->iSocket, pvBuf, cbRead, 0);
+    if (rc < 0)
+    {
+        *pcbRead = 0;
+        return -__libsocket_getSocketErrno();
+    }
+
+    *pcbRead = rc;
+    return 0;
+}
+
+/** Write operation.
+ * @returns 0 on success.
+ * @returns OS/2 error code or negated errno on failure.
+ * @param   pFH         Pointer to the handle structure to operate on.
+ * @param   fh          It's associated filehandle.
+ * @param   pvBuf       Pointer to the buffer which contains the data to write.
+ * @param   cbWrite     Number of bytes to write.
+ * @param   pcbWritten  Where to store the count of bytes actually written.
+ */
+static int __libsocket_ops_Write(PLIBCFH pFH, int fh, const void *pvBuf, size_t cbWrite, size_t *pcbWritten)
+{
+    PLIBCSOCKETFH   pSocketFH = (PLIBCSOCKETFH)pFH;
+    int             rc;
+
+    rc = __libsocket_send(pSocketFH->iSocket, pvBuf, cbWrite, 0);
+    if (rc < 0)
+    {
+        *pcbWritten = 0;
+        return -__libsocket_getSocketErrno();
+    }
+
+    *pcbWritten = 0;
+    return 0;
+
+}
+
+
+/** Duplicate handle operation.
+ * @returns 0 on success, OS/2 error code on failure.
+ * @param   pFH         Pointer to the handle structure to operate on.
+ * @param   fh          It's associated filehandle.
+ * @param   pfhNew      Where to store the duplicate filehandle.
+ *                      The input value describe how the handle is to be
+ *                      duplicated. If it's -1 a new handle is allocated.
+ *                      Any other value will result in that value to be
+ *                      used as handle. Any existing handle with that
+ *                      value will be closed.
+ */
+static int __libsocket_ops_Duplicate(PLIBCFH pFH, int fh, int *pfhNew)
+{
+    return -ENOSYS;
+}
+
+
+/** File Control operation.
+ * @returns 0 on success.
+ * @returns OS/2 error code or negated errno on failure.
+ * @param   pFH         Pointer to the handle structure to operate on.
+ * @param   fh          It's associated filehandle.
+ * @param   iIOControl  Which file file control operation to perform.
+ * @param   iArg        Argument which content is specific to each
+ *                      iIOControl operation.
+ * @param   prc         Where to store the value which upon success is
+ *                      returned to the caller.
+ */
+static int __libsocket_ops_FileControl(PLIBCFH pFH, int fh, int iIOControl, int iArg, int *prc)
+{
+    /*PLIBCSOCKETFH   pSocketFH = (PLIBCSOCKETFH)pFH;*/
+    return -ENOSYS;
+}
+
+
+/** I/O Control operation.
+ * @returns 0 on success.
+ * @returns OS/2 error code or negated errno on failure.
+ * @param   pFH         Pointer to the handle structure to operate on.
+ * @param   fh          It's associated filehandle.
+ * @param   iIOControl  Which I/O control operation to perform.
+ * @param   iArg        Argument which content is specific to each
+ *                      iIOControl operation.
+ * @param   prc         Where to store the value which upon success is
+ *                      returned to the caller.
+ */
+static int __libsocket_ops_IOControl(PLIBCFH pFH, int fh, int iIOControl, int iArg, int *prc)
+{
+    PLIBCSOCKETFH   pSocketFH = (PLIBCSOCKETFH)pFH;
+    char           *pchArg = (char *)iArg;
+    int             rc;
+#ifndef TCPV40HDRS
+    switch (__IOCLW(iIOControl))
+    {
+        case SIOSTATARP:
+            rc = __libsocket_os2_ioctl(pSocketFH->iSocket, iIOControl, pchArg, sizeof(struct oarptab));
+            break;
+        case SIOSTATAT:
+            /** this isn't really suitable for this ioctl interface!! */
+            rc = __libsocket_os2_ioctl(pSocketFH->iSocket, iIOControl, pchArg, sizeof(struct statatreq) + 2);
+            break;
+        case SIOSTATIF:
+            rc = __libsocket_os2_ioctl(pSocketFH->iSocket, iIOControl, pchArg, sizeof(struct ifmib));
+            break;
+        case SIOSTATIF42:
+            /* What the h*** is the difference between the SIOSTATIF42 ioctl and SIOSTATIF?
+               The docs doesn't make sense when looking in the headers... */
+            rc = __libsocket_os2_ioctl(pSocketFH->iSocket, iIOControl, pchArg, sizeof(struct ifmib));
+            break;
+        case SIOSTATRT:
+            rc = __libsocket_os2_ioctl(pSocketFH->iSocket, iIOControl, pchArg, sizeof(struct rtentries));
+            break;
+        case SIOSTATSO:
+            /** this isn't really suitable for this ioctl interface!! */
+            rc = __libsocket_os2_ioctl(pSocketFH->iSocket, iIOControl, pchArg, sizeof(struct sockaddr));
+            break;
+
+        case FIONBIO:
+            rc = __libsocket_ioctl(pSocketFH->iSocket, iIOControl, pchArg);
+            if (rc)
+            {
+                if (*(unsigned*)pchArg)
+                    pSocketFH->core.fFlags |= O_NDELAY;
+                else
+                    pSocketFH->core.fFlags &= ~O_NDELAY;
+            }
+            break;
+
+        default:
+            rc = __libsocket_ioctl(pSocketFH->iSocket, iIOControl, pchArg);
+            break;
+    }
+
+#else
+    int     cb;
+
+    /*
+     * This ioctl interface requires a size, which thus is something we
+     * must determin. :-/
+     */
+    switch (__IOCLW(iIOControl))
+    {
+        case __IOCLW(FIONREAD):
+        case __IOCLW(FIOASYNC):
+        case __IOCLW(SIOCATMARK):
+        case __IOCLW(FIONBIO):
+        case __IOCLW(SIOCSHIWAT):
+        case __IOCLW(SIOCGHIWAT):
+        case __IOCLW(SIOCSLOWAT):
+        case __IOCLW(SIOCGLOWAT):
+        case __IOCLW(SIOCSPGRP):
+        case __IOCLW(SIOCGPGRP):
+            cb = sizeof(int);
+            break;
+
+        case __IOCLW(SIOCSHOSTID):
+        case __IOCLW(SIOCGNBNAME):
+        case __IOCLW(SIOCSNBNAME):
+        case __IOCLW(SIOCGNCBFN):
+            cb = sizeof(long);
+            break;
+
+        case __IOCLW(FIONSTATUS):
+            cb = sizeof(short) * 4;
+            break;
+
+        case __IOCLW(FIOBSTATUS):
+            cb = sizeof(short);
+            break;
+
+        case __IOCLW(SIOCGIFADDR):
+        case __IOCLW(SIOCGIFBRDADDR):
+        case __IOCLW(SIOCGIFDSTADDR):
+        case __IOCLW(SIOCGIFFLAGS):
+        case __IOCLW(SIOCGIFMETRIC):
+        case __IOCLW(SIOCGIFNETMASK):
+        case __IOCLW(SIOCSIFADDR):
+        case __IOCLW(SIOCSIFBRDADDR):
+        case __IOCLW(SIOCSIFDSTADDR):
+        case __IOCLW(SIOCSIFFLAGS):
+        case __IOCLW(SIOCSIFMETRIC):
+        case __IOCLW(SIOCSIFNETMASK):
+        case __IOCLW(SIOCSIFALLRTB):
+        case __IOCLW(SIOCSIF802_3):
+        case __IOCLW(SIOCSIFNO802_3):
+        case __IOCLW(SIOCSIFNOREDIR):
+        case __IOCLW(SIOCSIFYESREDIR):
+        case __IOCLW(SIOCSIFMTU):
+        case __IOCLW(SIOCSIFFDDI):
+        case __IOCLW(SIOCSIFNOFDDI):
+        case __IOCLW(SIOCSRDBRD):
+        case __IOCLW(SIOCADDMULTI):
+        case __IOCLW(SIOCDELMULTI):
+        case __IOCLW(SIOCMULTISBC):
+        case __IOCLW(SIOCMULTISFA):
+        case __IOCLW(SIOCDIFADDR):
+        case __IOCLW(SIOCGUNIT):
+        case __IOCLW(SIOCGIFVALID):
+        case __IOCLW(SIOCGIFEFLAGS):
+        case __IOCLW(SIOCSIFEFLAGS):
+        case __IOCLW(SIOCGIFTRACE):
+        case __IOCLW(SIOCSIFTRACE):
+        case __IOCLW(SIOCSSTAT):
+        case __IOCLW(SIOCGSTAT):
+            cb = sizeof(struct ifreq);
+            break;
+
+        case __IOCLW(SIOCADDRT):
+        case __IOCLW(SIOCDELRT):
+            cb = sizeof(struct rtentry);
+            break;
+
+        case __IOCLW(SIOCSARP):
+        case __IOCLW(SIOCGARP):
+        case __IOCLW(SIOCDARP):
+            cb = sizeof(struct arpreq);
+            break;
+
+        case __IOCLW(SIOCGIFCONF):
+            cb = sizeof(struct ifconf);
+            break;
+
+        case __IOCLW(SIOCSARP_TR):
+        case __IOCLW(SIOCGARP_TR):
+            cb = sizeof(struct arpreq_tr);
+            break;
+
+        /* duplicate! Bug in BSD 4.3 stack I'd say.
+        case __IOCLW(SIOCAIFADDR):
+            cb = sizeof(struct ifaliasreq);
+            break;
+         */
+
+        case __IOCLW(SIOCGIFBOUND):
+            cb = sizeof(struct bndreq);
+            break;
+
+        case __IOCLW(SIOCGMCAST):
+            cb = sizeof(struct addrreq);
+            break;
+
+        case __IOCLW(SIOSTATMBUF):
+            cb = sizeof(struct mbstat);
+            break;
+
+        case __IOCLW(SIOSTATTCP):
+            cb = sizeof(struct tcpstat);
+            break;
+
+        case __IOCLW(SIOSTATUDP):
+            cb = sizeof(struct udpstat);
+            break;
+
+        case __IOCLW(SIOSTATIP):
+            cb = sizeof(struct ipstat);
+            break;
+
+        case __IOCLW(SIOSTATSO):
+            cb = sizeof(struct sockaddr);
+            break;
+
+        case __IOCLW(SIOSTATRT):
+            cb = sizeof(struct rtentries);
+            break;
+
+        case __IOCLW(SIOFLUSHRT):
+            cb = sizeof(long);
+            break;
+
+        case __IOCLW(SIOSTATICMP):
+            cb = sizeof(struct icmpstat);
+            break;
+
+        case __IOCLW(SIOSTATIF):
+            cb = sizeof(struct ifmib);
+            break;
+
+        case __IOCLW(SIOSTATAT):
+            /** this isn't really suitable for this ioctl interface!! */
+            cb = sizeof(struct statatreq) + 2;
+            break;
+
+        case __IOCLW(SIOSTATARP):
+            cb = sizeof(struct oarptab);
+            break;
+
+        case __IOCLW(SIOSTATIF42):
+            cb = sizeof(struct ifmib); /* docs are unclear here */
+            break;
+
+        case __IOCLW(SIOSTATCNTRT):
+            cb = sizeof(int);
+            break;
+
+        case __IOCLW(SIOSTATCNTAT):
+            cb = sizeof(int);
+            break;
+
+        default:
+            cb = -1;
+            break;
+
+/** @todo There is a bunch of IOCtls in the BSD 4.3 stack
+ *        for which I don't have the docs and don't know exact
+ *        what sizes to use:
+ *               FIOTCPCKSUM     ioc('f', 128)
+ *               FIONURG         ioc('f', 121)
+ *               SIOMETRIC1RT    ioc('r', 12)
+ *               SIOMETRIC2RT    ioc('r', 13)
+ *               SIOMETRIC3RT    ioc('r', 14)
+ *               SIOMETRIC4RT    ioc('r', 15)
+ *               SIOCREGADDNET   ioc('r', 12)
+ *               SIOCREGDELNET   ioc('r', 13)
+ *               SIOCREGROUTES   ioc('r', 14)
+ *               SIOCFLUSHROUTES ioc('r', 15)
+ *               SIOCSIFSETSIG   ioc('i', 25)
+ *               SIOCSIFCLRSIG   ioc('i', 26)
+ *               SIOCSIFBRD      ioc('i', 27)
+ *               SIOCGIFLOAD     ioc('i', 27)
+ *               SIOCSIFFILTERSRC ioc('i', 28)
+ *               SIOCGIFFILTERSRC ioc('i',29)
+ *               SIOCSIFSNMPSIG  ioc('i', 33)
+ *               SIOCSIFSNMPCLR  ioc('i', 34)
+ *               SIOCSIFSNMPCRC  ioc('i', 35)
+ *               SIOCSIFPRIORITY ioc('i', 36)
+ *               SIOCGIFPRIORITY ioc('i', 37)
+ *               SIOCSIFFILTERDST ioc('i', 38)
+ *               SIOCGIFFILTERDST ioc('i',39)
+ *               SIOCSIFSPIPE     ioc('i',71)
+ *               SIOCSIFRPIPE     ioc('i',72)
+ *               SIOCSIFTCPSEG   ioc('i',73)
+ *               SIOCSIFUSE576   ioc('i',74)
+ */
+    } /* size switch */
+
+    /*
+     * Do the call.
+     */
+    if (cb >= 0)
+    {
+        switch (__IOCLW(iIOControl))
+        {
+            case FIONBIO:
+                rc = __libsocket_tcpipv4_ioctl(pSocketFH->iSocket, iIOControl, pchArg, cb);
+                if (rc)
+                {
+                    if (*(unsigned*)pchArg)
+                        pSocketFH->core.fFlags |= O_NDELAY;
+                    else
+                        pSocketFH->core.fFlags &= ~O_NDELAY;
+                }
+                break;
+
+            default:
+                rc = __libsocket_tcpipv4_ioctl(pSocketFH->iSocket, iIOControl, pchArg, cb);
+                break;
+        }
+    }
+    else
+    {   /*  */
+        *prc = -1;
+        return -ENOSYS;
+    }
+
+#endif
+
+    *prc = rc;
+    if (rc < 0)
+        return -__libsocket_getSocketErrno();
+    return 0;
+
+    return -ENOSYS;
+}
Index: trunk/src/emx/src/libsocket/socketpair.c
===================================================================
--- trunk/src/emx/src/libsocket/socketpair.c	(revision 885)
+++ trunk/src/emx/src/libsocket/socketpair.c	(revision 886)
@@ -12,5 +12,9 @@
  */
 
-#define TCPV40HDRS
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
 #include <sys/types.h>
 #include <sys/time.h>
@@ -20,4 +24,9 @@
 #include <stdio.h>
 #include <string.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
 
 
@@ -35,4 +44,5 @@
 int     TCPCALL socketpair(int af, int type, int flags, int *osfd)
 {
+#ifdef TCPV40HDRS
     int     rc;
     int     sock1 = -1;
@@ -106,4 +116,40 @@
         soclose(sock2);
     return -1;
+
+#else
+    int             rc;
+    int             fh1;
+    int             fh2;
+    PLIBCSOCKETFH   pFH;
+
+    rc = __libsocket_pair(af, type, flags, osfd);
+    if (rc < 0)
+    {
+        __libsocket_setLibcErrno();
+        return -1;
+    }
+
+    /*
+     * Allocate LIBC sockets.
+     */
+    rc = __libc_FHAllocate(-1, O_RDWR | F_SOCKET, sizeof(LIBCSOCKETFH), &__libsocket_gSocketOps, (PLIBCFH*)&pFH, &fh1);
+    if (!rc)
+    {
+        pFH->iSocket = osfd[0];
+        rc = __libc_FHAllocate(-1, O_RDWR | F_SOCKET, sizeof(LIBCSOCKETFH), &__libsocket_gSocketOps, (PLIBCFH*)&pFH, &fh2);
+        if (!rc)
+        {
+            pFH->iSocket = osfd[1];
+            osfd[0] = fh1;
+            osfd[1] = fh2;
+            return 0;
+        }
+        __libc_FHClose(fh1);
+    }
+
+    __libsocket_soclose(osfd[0]);
+    __libsocket_soclose(osfd[1]);
+    return -1;
+#endif
 }
 
Index: trunk/src/emx/src/libsocket/soclose.c
===================================================================
--- trunk/src/emx/src/libsocket/soclose.c	(revision 886)
+++ trunk/src/emx/src/libsocket/soclose.c	(revision 886)
@@ -0,0 +1,49 @@
+/* $Id$ */
+/** @file
+ *
+ * soclose().
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "libc-alias.h"
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <emx/io.h>
+#include "socket.h"
+
+int soclose(int socket)
+{
+    PLIBCSOCKETFH   pFHSocket = __libsocket_FH(socket);
+    if (pFHSocket)
+    {
+        if (!__libc_FHClose(socket))
+            return 0;
+        __libsocket_setSocketErrno();
+    }
+
+    return -1;
+}
+
