/*
    Title:      Network functions.

	Copyright (c) 2000 David C. J. Matthews

	This library 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.1 of the License, or (at your option) any later version.
	
	This library 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 this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
#include <errno.h>
#include <assert.h>
#include <string.h>

#ifdef WINDOWS_PC
#ifdef USEWINSOCK2
#include <winsock2.h>
#else
#include <winsock.h>
#endif
#else
#include <unistd.h>
#include <sys/param.h>
#include <sys/time.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/ioctl.h>
#include <sys/un.h>
#ifndef LINUX
#include <sys/filio.h>
#include <sys/sockio.h>
#endif
#endif

#include "globals.h"
#include "objects.h"
#include "gc.h"
#include "arb.h"
#include "run_time.h"
#include "mpoly.h"
#include "processes.h"
#include "proper_io.h"
#include "mmap.h"
#include "memory.h"
#include "network.h"
#include "io_internal.h"
#include "sys.h"

#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 256
#endif

#define STREAMID(x) (*DEREFSTREAMHANDLE(x))
#define SAVE(x) push_to_save_vec((word)(x))
#define ALLOC(n) alloc_and_save(n)
#define SIZEOF(x) (sizeof(x)/sizeof(word))
#define EMPTYSTRING interface_map[POLY_SYS_nullorzero]

#ifdef WINDOWS_PC
static int winsock_init = 0; /* Check that it has been initialised. */
/* EWOULDBLOCK is defined as negative because GetError returns
   negative values for socket library errors which do not have
   equivalents as errno-style errors. */
#define EWOULDBLOCK (-WSAEWOULDBLOCK)
#define EINPROGRESS (-WSAEWOULDBLOCK)
#else
#define INVALID_SOCKET (-1)
#define SOCKET_ERROR	(-1)
#endif

#ifndef SHUT_RD
#define SHUT_RD		0
#endif

#ifndef SHUT_WR
#define SHUT_WR		1
#endif

#ifndef SHUT_RDWR
#define SHUT_RDWR	2
#endif

/* Address families.  Although this table is in ascending
   numerical order of address family nothing depends on that.
   The only requirement is that "INET" => AF_INET must always
   be present and "UNIX" => AF_UNIX must be present on Unix.
   Other entries are entirely optional and are for amusement
   only. */
struct af_tab_struct {
	char *af_name;
	int af_num;
} af_table[] =
{
#ifdef AF_UNIX
	{ "UNIX",		AF_UNIX }, /* This is nearly always there. */
#endif
#ifdef AF_LOCAL
	{ "LOCAL",		AF_LOCAL },
#endif
	{ "INET",		AF_INET }, /* This one should always be there. */
#ifdef AF_IMPLINK
	{ "IMPLINK",		AF_IMPLINK },
#endif
#ifdef AF_PUP
	{ "PUP",		AF_PUP },
#endif
#ifdef AF_CHAOS
	{ "CHAOS",		AF_CHAOS },
#endif
#ifdef AF_IPX
	{ "IPX",		AF_IPX },
#endif
#ifdef AF_NS
	{ "NS",			AF_NS },
#endif
#ifdef AF_ISO
	{ "ISO",		AF_ISO },
#endif
#ifdef AF_OSI
	{ "OSI",		AF_OSI },
#endif
#ifdef AF_ECMA
	{ "ECMA",		AF_ECMA },
#endif
#ifdef AF_DATAKIT
	{ "DATAKIT",		AF_DATAKIT },
#endif
#ifdef AF_CCITT
	{ "CCITT",		AF_CCITT },
#endif
#ifdef AF_SNA
	{ "SNA",		AF_SNA },
#endif
#ifdef AF_DECnet
	{ "DECnet",		AF_DECnet },
#endif
#ifdef AF_DLI
	{ "DLI",		AF_DLI },
#endif
#ifdef AF_LAT
	{ "LAT",		AF_LAT },
#endif
#ifdef AF_HYLINK
	{ "HYLINK",		AF_HYLINK },
#endif
#ifdef AF_APPLETALK
	{ "APPLETALK",		AF_APPLETALK },
#endif
#ifdef AF_NETBIOS
	{ "NETBIOS",		AF_NETBIOS },
#endif
#ifdef AF_ROUTE
	{ "ROUTE",		AF_ROUTE },
#endif
#ifdef AF_VOICEVIEW
	{ "VOICEVIEW",		AF_VOICEVIEW },
#endif
#ifdef AF_FIREFOX
	{ "FIREFOX",		AF_FIREFOX },
#endif
#ifdef AF_BAN
	{ "BAN",		AF_BAN },
#endif
#ifdef AF_LINK
	{ "LINK",		AF_LINK },
#endif
#ifdef AF_COIP
	{ "COIP",		AF_COIP },
#endif
#ifdef AF_CNT
	{ "CNT",		AF_CNT },
#endif
#ifdef AF_SIP
	{ "SIP",		AF_SIP },
#endif
#ifdef AF_ISDN
	{ "ISDN",		AF_ISDN },
#endif
#ifdef AF_E164
	{ "E164",		AF_E164 },
#endif
#ifdef AF_INET6
	{ "INET6",		AF_INET6 },
#endif
#ifdef AF_NATM
	{ "NATM",		AF_NATM },
#endif
#ifdef AF_ATM
	{ "ATM",		AF_ATM },
#endif
#ifdef AF_NETGRAPH
	{ "NETGRAPH",		AF_NETGRAPH },
#endif
};

/* Socket types.  Only STREAM and DGRAM are required.  */
struct sk_tab_struct {
	char *sk_name;
	int sk_num;
} sk_table[] =
{
	{ "STREAM",		SOCK_STREAM },
	{ "DGRAM",		SOCK_DGRAM },
	{ "RAW",		SOCK_RAW },
	{ "RDM",		SOCK_RDM },
	{ "SEQPACKET",		SOCK_SEQPACKET }
};

static Handle makeHostEntry(struct hostent *host);
static Handle makeProtoEntry(struct protoent *proto);
static Handle makeServEntry(struct servent *proto);
static Handle makeNetEntry(struct netent *net);
static Handle makeList(int count, char *p, int size, void *arg,
					   Handle (mkEntry)(void*, char*));
static Handle mkAftab(void*, char *p);
static Handle mkSktab(void*, char *p);
static Handle setSocketOption(Handle args, int level, int opt);
static Handle getSocketOption(Handle args, int level, int opt);
static Handle getSocketInt(Handle args, int level, int opt);
static Handle selectCall(Handle args, int blockType);

#ifdef WINDOWS_PC
/* To allow for portable code we map Windows socket errors to
   errno-style errors if there is a suitable entry in errno.h.
   If there isn't we simply return the negative value and handle
   it later. N.B. WSAEWOULDBLOCK is one of those which does not
   have an equivalent. */
static int MapError(int err)
{
	switch (err)
	{
	case WSAEINTR: return EINTR;
	case WSAEBADF: return EBADF;
	case WSAEACCES: return EACCES;
	case WSAEFAULT: return EFAULT;
	case WSAEINVAL: return EINVAL;
	case WSAEMFILE: return EMFILE;
	case WSAENAMETOOLONG: return ENAMETOOLONG;
	case WSAENOTEMPTY: return ENOTEMPTY;
	default: return -err;
	}
}

static int GetError()
{
	return MapError(WSAGetLastError());
}
#define	GETERROR	(GetError())
#define MAPERROR(x)	(MapError(x))
#else
#define	GETERROR	(errno)
#define MAPERROR(x)	(x)
#endif


Handle Net_dispatch_c(Handle args, Handle code)
{
	int c = get_C_long(DEREFWORDHANDLE(code));
TryAgain:
	switch (c)
	{
	case 0:
		{ /* Get the current host name. */
			char hostName[MAXHOSTNAMELEN];
			if (gethostname(hostName, MAXHOSTNAMELEN) != 0)
				raise_syscall("gethostname failed", GETERROR);
			return (SAVE(C_string_to_Poly(hostName)));
		}

	case 1:
		{
			/* Look up a host name. */
			char hostName[MAXHOSTNAMELEN];
			int length;
			struct hostent *host;
			length = Poly_string_to_C(DEREFSTRINGHANDLE(args),
						hostName, MAXHOSTNAMELEN);
			if (length > MAXHOSTNAMELEN)
				raise_syscall("Host name too long", ENAMETOOLONG);
			host = gethostbyname(hostName);
			if (host == NULL)
				raise_syscall("gethostbyname failed", GETERROR);
			return makeHostEntry(host);
		}

	case 2:
		{
			/* Look up entry by address. */
			unsigned long addr =
				htonl(get_C_ulong(DEREFWORDHANDLE(args)));
			struct hostent *host;
			/* Look up a host name given an address. */
			host = gethostbyaddr((char*)&addr, sizeof(addr), AF_INET);
			if (host == NULL)
				raise_syscall("gethostbyaddr failed", GETERROR);
			return makeHostEntry(host);
		}

	case 3:
		{
			/* Look up protocol entry. */
			char protoName[MAXHOSTNAMELEN];
			int length;
			struct protoent *proto;
			length = Poly_string_to_C(DEREFSTRINGHANDLE(args),
						protoName, MAXHOSTNAMELEN);
			if (length > MAXHOSTNAMELEN)
				raise_syscall("Protocol name too long", ENAMETOOLONG);
			proto = getprotobyname(protoName);
			if (proto == NULL)
				raise_syscall("getprotobyname failed", GETERROR);
			return makeProtoEntry(proto);
		}

	case 4:
		{
			/* Look up protocol entry. */
			struct protoent *proto;
			int pNum = get_C_ulong(DEREFWORDHANDLE(args));
			proto = getprotobynumber(pNum);
			if (proto == NULL)
				raise_syscall("getprotobynumber failed", GETERROR);
			return makeProtoEntry(proto);
		}

	case 5:
		{
			/* Get service given service name only. */
			char servName[MAXHOSTNAMELEN];
			int length;
			struct servent *serv;
			length = Poly_string_to_C(DEREFSTRINGHANDLE(args),
						servName, MAXHOSTNAMELEN);
			if (length > MAXHOSTNAMELEN)
				raise_syscall("Service name too long", ENAMETOOLONG);
			serv = getservbyname (servName, NULL);
			if (serv == NULL)
				raise_syscall("getservbyname failed", GETERROR);
			return makeServEntry(serv);
		}

	case 6:
		{
			/* Get service given service name and protocol name. */
			char servName[MAXHOSTNAMELEN], protoName[MAXHOSTNAMELEN];
			int length;
			struct servent *serv;
			length = Poly_string_to_C((pstring)DEREFHANDLE(args)[0],
						servName, MAXHOSTNAMELEN);
			if (length > MAXHOSTNAMELEN)
				raise_syscall("Service name too long", ENAMETOOLONG);
			length = Poly_string_to_C((pstring)DEREFHANDLE(args)[1],
						protoName, MAXHOSTNAMELEN);
			if (length > MAXHOSTNAMELEN)
				raise_syscall("Protocol name too long", ENAMETOOLONG);
			serv = getservbyname (servName, protoName);
			if (serv == NULL)
				raise_syscall("getservbyname failed", GETERROR);
			return makeServEntry(serv);
		}

	case 7:
		{
			/* Get service given port number only. */
			struct servent *serv;
			long port = htons(get_C_ushort(DEREFWORDHANDLE(args)));
			serv = getservbyport(port, NULL);
			if (serv == NULL)
				raise_syscall("getservbyport failed", GETERROR);
			return makeServEntry(serv);
		}

	case 8:
		{
			/* Get service given port number and protocol name. */
			char protoName[MAXHOSTNAMELEN];
			int length;
			struct servent *serv;
			long port = htons(get_C_ushort(DEREFHANDLE(args)[0]));
			length = Poly_string_to_C((pstring)DEREFHANDLE(args)[1],
						protoName, MAXHOSTNAMELEN);
			if (length > MAXHOSTNAMELEN)
				raise_syscall("Protocol name too long", ENAMETOOLONG);
			serv = getservbyport (port, protoName);
			if (serv == NULL)
				raise_syscall("getservbyport failed", GETERROR);
			return makeServEntry(serv);
		}

	case 9:
		{
			/* Get a network entry. */
			char netName[MAXHOSTNAMELEN];
			int length;
			struct netent *net = NULL;
			length = Poly_string_to_C(DEREFSTRINGHANDLE(args),
						netName, MAXHOSTNAMELEN);
			if (length > MAXHOSTNAMELEN)
				raise_syscall("Network name too long", ENAMETOOLONG);
#ifndef WINDOWS_PC
			/* Winsock2 does not have getnetbyname.
			   Just return failure. */
			net = getnetbyname(netName);
#endif
			if (net == NULL)
				raise_syscall("getnetbyname failed", GETERROR);
			return makeNetEntry(net);
		}

	case 10:
		{
			/* Look up network entry from a number. */
			struct netent *net = NULL;
			unsigned long netNum = htonl(get_C_ulong(DEREFHANDLE(args)[0]));
			long af = get_C_ulong(DEREFHANDLE(args)[0]);
#ifndef WINDOWS_PC
			/* Winsock2 does not have getnetbyaddr.
			   Just return failure. */
			net = getnetbyaddr(netNum, af);
#endif
			if (net == NULL)
				raise_syscall("getnetbyaddr failed", GETERROR);
			return makeNetEntry(net);
		}

	case 11:
		{
			/* Return a list of known address families. */
			return makeList(sizeof(af_table)/sizeof(af_table[0]),
							(char*)af_table, sizeof(af_table[0]),
							0, mkAftab);
		}

	case 12:
		{
			/* Return a list of known socket types. */
			return makeList(sizeof(sk_table)/sizeof(sk_table[0]),
							(char*)sk_table, sizeof(sk_table[0]),
							0, mkSktab);
		}

	case 13: /* Return the "any" internet address. */
		return Make_unsigned(INADDR_ANY);

	case 14: /* Create a socket */
		{
			Handle str_token = make_stream_entry();
			PIOSTRUCT strm;
			int stream_no = STREAMID(str_token);
			int af = get_C_long(DEREFHANDLE(args)[0]);
			int type = get_C_long(DEREFHANDLE(args)[1]);
			int proto = get_C_long(DEREFHANDLE(args)[2]);
			int onOff = 1;
			SOCKET skt = socket(af, type, proto);
			if (skt == INVALID_SOCKET)
			{
				free_stream_entry(stream_no);
				switch (GETERROR)
				{
				case EMFILE: /* too many open files */
					{
						if (emfileFlag) /* Previously had an EMFILE error. */
							raise_syscall("socket failed", EMFILE);
						emfileFlag = 1;
						FullGC(); /* May clear emfileFlag if we close a file. */
						goto TryAgain;
					}
				case EINTR: goto TryAgain;
				default: raise_syscall("socket failed", GETERROR);
				}
			}
			/* Set the socket to non-blocking mode. */
#ifdef WINDOWS_PC
			if (ioctlsocket(skt, FIONBIO, &onOff) != 0)
#else
			if (proper_ioctl(skt, FIONBIO, &onOff) < 0)
#endif
			{
				free_stream_entry(stream_no);
#ifdef WINDOWS_PC
				closesocket(skt);
#else
				close(skt);
#endif
				raise_syscall("ioctl failed", GETERROR);
			}
			strm = &basic_io_vector[stream_no];
			strm->device.sock = skt;
			strm->ioBits =
				IO_BIT_OPEN | IO_BIT_READ | IO_BIT_WRITE | IO_BIT_SOCKET ;
			return(str_token);
 		}

	case 15: /* Set TCP No-delay option. */
		return setSocketOption(args, IPPROTO_TCP, TCP_NODELAY);

	case 16: /* Get TCP No-delay option. */
		return getSocketOption(args, IPPROTO_TCP, TCP_NODELAY);

	case 17: /* Set Debug option. */
		return setSocketOption(args, SOL_SOCKET, SO_DEBUG);

	case 18: /* Get Debug option. */
		return getSocketOption(args, SOL_SOCKET, SO_DEBUG);

	case 19: /* Set REUSEADDR option. */
		return setSocketOption(args, SOL_SOCKET, SO_REUSEADDR);

	case 20: /* Get REUSEADDR option. */
		return getSocketOption(args, SOL_SOCKET, SO_REUSEADDR);

	case 21: /* Set KEEPALIVE option. */
		return setSocketOption(args, SOL_SOCKET, SO_KEEPALIVE);

	case 22: /* Get KEEPALIVE option. */
		return getSocketOption(args, SOL_SOCKET, SO_KEEPALIVE);

	case 23: /* Set DONTROUTE option. */
		return setSocketOption(args, SOL_SOCKET, SO_DONTROUTE);

	case 24: /* Get DONTROUTE option. */
		return getSocketOption(args, SOL_SOCKET, SO_DONTROUTE);

	case 25: /* Set BROADCAST option. */
		return setSocketOption(args, SOL_SOCKET, SO_BROADCAST);

	case 26: /* Get BROADCAST option. */
		return getSocketOption(args, SOL_SOCKET, SO_BROADCAST);

	case 27: /* Set OOBINLINE option. */
		return setSocketOption(args, SOL_SOCKET, SO_OOBINLINE);

	case 28: /* Get OOBINLINE option. */
		return getSocketOption(args, SOL_SOCKET, SO_OOBINLINE);

	case 29: /* Set SNDBUF size. */
		return setSocketOption(args, SOL_SOCKET, SO_SNDBUF);

	case 30: /* Get SNDBUF size. */
		return getSocketInt(args, SOL_SOCKET, SO_SNDBUF);

	case 31: /* Set RCVBUF size. */
		return setSocketOption(args, SOL_SOCKET, SO_RCVBUF);

	case 32: /* Get RCVBUF size. */
		return getSocketInt(args, SOL_SOCKET, SO_RCVBUF);

	case 33: /* Get socket type e.g. SOCK_STREAM. */
		return getSocketInt(args, SOL_SOCKET, SO_TYPE);

	case 34: /* Get error status and clear it. */
		return getSocketOption(args, SOL_SOCKET, SO_ERROR);

	case 35: /* Set Linger time. */
		{
			struct linger linger;
			PIOSTRUCT strm = get_stream((Handle)&DEREFHANDLE(args)[0]);
			int lTime = get_C_long(DEREFHANDLE(args)[1]);
			/* We pass in a negative value to turn the option off,
			   zero or positive to turn it on. */
			if (lTime < 0)
			{
				linger.l_onoff = 0;
				linger.l_linger = 0;
			}
			else
			{
				linger.l_onoff = 1;
				linger.l_linger = lTime;
			}
			if (strm == NULL) raise_syscall("Stream is closed", EBADF);
			if (setsockopt(strm->device.sock, SOL_SOCKET, SO_LINGER,
				(char*)&linger, sizeof(linger)) != 0)
				raise_syscall("setsockopt failed", GETERROR);
			return Make_arbitrary_precision(0);
		}

	case 36: /* Get Linger time. */
		{
			struct linger linger;
			PIOSTRUCT strm = get_stream(args);
			int size = sizeof(linger), lTime = 0;
			if (strm == NULL) raise_syscall("Stream is closed", EBADF);
			if (getsockopt(strm->device.sock, SOL_SOCKET, SO_LINGER,
				(char*)&linger, &size) != 0)
				raise_syscall("getsockopt failed", GETERROR);
			/* If the option is off return a negative. */
			if (linger.l_onoff == 0) lTime = -1;
			else lTime = linger.l_linger;
			return Make_arbitrary_precision(lTime);
		}

	case 37: /* Get peer name. */
		{
			PIOSTRUCT strm = get_stream(args);
			struct sockaddr sockA;
			int	size = sizeof(sockA);
			if (strm == NULL) raise_syscall("Stream is closed", EBADF);
			if (getpeername(strm->device.sock, &sockA, &size) != 0)
				raise_syscall("getpeername failed", GETERROR);
			/* Addresses are treated as strings. */
			return(SAVE(Buffer_to_Poly((char*)&sockA, size)));
		}

	case 38: /* Get socket name. */
		{
			PIOSTRUCT strm = get_stream(args);
			struct sockaddr sockA;
			int	size = sizeof(sockA);
			if (strm == NULL) raise_syscall("Stream is closed", EBADF);
			if (getsockname(strm->device.sock, &sockA, &size) != 0)
				raise_syscall("getsockname failed", GETERROR);
			return(SAVE(Buffer_to_Poly((char*)&sockA, size)));
		}

	case 39: /* Return the address family from an address. */
		{
			pstring psAddr = DEREFSTRINGHANDLE(args);
			struct sockaddr *psock = (struct sockaddr *)&psAddr->chars;
			return Make_unsigned(psock->sa_family);
		}

	case 40: /* Create a socket address from a port number and
				internet address. */
		{
			struct sockaddr_in sockaddr;
			memset(&sockaddr, 0, sizeof(sockaddr));
			sockaddr.sin_family = AF_INET;
			sockaddr.sin_port = htons(get_C_ushort(DEREFHANDLE(args)[0]));
			sockaddr.sin_addr.s_addr =
				htonl(get_C_ulong(DEREFHANDLE(args)[1]));
			return(SAVE(Buffer_to_Poly((char*)&sockaddr, sizeof(sockaddr))));
		}

	case 41: /* Return port number from an internet socket address.
				Assumes that we've already checked the address family. */
		{
			pstring psAddr = DEREFSTRINGHANDLE(args);
			struct sockaddr_in *psock =
				(struct sockaddr_in *)&psAddr->chars;
			return Make_unsigned(ntohs(psock->sin_port));
		}

	case 42: /* Return internet address from an internet socket address.
				Assumes that we've already checked the address family. */
		{
			pstring psAddr = DEREFSTRINGHANDLE(args);
			struct sockaddr_in *psock =
				(struct sockaddr_in *)&psAddr->chars;
			return Make_unsigned(ntohl(psock->sin_addr.s_addr));
		}

        /* 43 - Set non-blocking mode.  Now removed. */

	case 44: /* Find number of bytes available. */
		{
			PIOSTRUCT strm = get_stream(args);
			long readable;
			if (strm == NULL) raise_syscall("Stream is closed", EBADF);
#ifdef WINDOWS_PC
			if (ioctlsocket(strm->device.sock, FIONREAD, &readable) != 0)
				raise_syscall("ioctlsocket failed", GETERROR);
#else
			if (proper_ioctl(strm->device.sock, FIONREAD, &readable) < 0)
				raise_syscall("ioctl failed", GETERROR);
#endif
			return Make_arbitrary_precision(readable);
		}

	case 45: /* Find out if we are at the mark. */
		{
			PIOSTRUCT strm = get_stream(args);
			long atMark;
			if (strm == NULL) raise_syscall("Stream is closed", EBADF);
#ifdef WINDOWS_PC
			if (ioctlsocket(strm->device.sock, SIOCATMARK, &atMark) != 0)
				raise_syscall("ioctlsocket failed", GETERROR);
#else
			if (proper_ioctl(strm->device.sock, SIOCATMARK, &atMark) < 0)
				raise_syscall("ioctl failed", GETERROR);
#endif
			return Make_arbitrary_precision(atMark == 0 ? 0 : 1);
		}

	case 46: /* Accept a connection. */
    case 58: /* Non-blocking accept. */
		{
			PIOSTRUCT strm = get_stream(args);
			if (strm == NULL) raise_syscall("Stream is closed", EBADF);
			else {
				SOCKET sock = strm->device.sock, result;
				struct sockaddr resultAddr;
				int stream_no, addrLen;
				Handle addrHandle, pair;
				Handle str_token;
				PIOSTRUCT newStrm;
				/* Get a token for the new socket - may raise an
				   exception if it fails. */
				str_token = make_stream_entry();
				stream_no = STREAMID(str_token);
				addrLen = sizeof(resultAddr);
				result = accept(sock, &resultAddr, &addrLen);

				if (result == INVALID_SOCKET)
				{
					/* Free the stream entry */
					free_stream_entry(stream_no);
					switch (GETERROR)
					{
					case EINTR:
						goto TryAgain; /* Have to retry if we got EINTR. */
					case EMFILE: /* Too many files. */
						{
							if (emfileFlag) /* Previously had an EMFILE error. */
								raise_syscall("accept failed", EMFILE);
							emfileFlag = 1;
							FullGC(); /* May clear emfileFlag if we close a file. */
							goto TryAgain;
						}
					case EWOULDBLOCK:
						/* If the socket is in non-blocking mode we pass
						   this back to the caller.  If it is blocking we
						   suspend this process and try again later. */
						if (c == 46 /* blocking version. */)
							block_and_restart(strm->device.sock, 0, POLY_SYS_network);
						/* else drop through. */
					default:
						raise_syscall("accept failed", GETERROR);
					}
				}

				addrHandle = SAVE(Buffer_to_Poly((char*)&resultAddr, addrLen));
				newStrm = &basic_io_vector[stream_no];
				newStrm->device.sock = result;
				newStrm->ioBits =
					IO_BIT_OPEN | IO_BIT_READ | IO_BIT_WRITE | IO_BIT_SOCKET;
				/* Return a pair of the new socket and the address. */
				pair = ALLOC(2);
				DEREFHANDLE(pair)[0] = DEREFWORDHANDLE(str_token);
				DEREFHANDLE(pair)[1] = DEREFWORDHANDLE(addrHandle);
				return pair;
			}
		}

	case 47: /* Bind an address to a socket. */
		{
			PIOSTRUCT strm = get_stream((Handle)&DEREFHANDLE(args)[0]);
			pstring psAddr = (pstring)DEREFHANDLE(args)[1];
			struct sockaddr *psock = (struct sockaddr *)&psAddr->chars;
			if (strm == NULL) raise_syscall("Stream is closed", EBADF);
			if (bind(strm->device.sock, psock, psAddr->length) != 0)
				raise_syscall("bind failed", GETERROR);
			return Make_arbitrary_precision(0);
		}

	case 48: /* Connect to an address. */
    case 59: /* Non-blocking connect. */
		{
			PIOSTRUCT strm = get_stream((Handle)&DEREFHANDLE(args)[0]);
			pstring psAddr = (pstring)DEREFHANDLE(args)[1];
			struct sockaddr *psock = (struct sockaddr *)&psAddr->chars;
			int res;
			if (strm == NULL) raise_syscall("Stream is closed", EBADF);
			/* In Windows, and possibly also in Unix, if we have
			   received a previous EWOULDBLOCK we have to use "select"
			   to tell us whether the connection actually succeeded. */
			while (1)
			{
				if (strm->ioBits & IO_BIT_INPROGRESS)
				{
					fd_set read_fds, write_fds, except_fds;
					struct timeval delay;
					int sel;
					SOCKET sock = strm->device.sock;
					FD_ZERO(&read_fds);
					FD_ZERO(&write_fds);
					FD_ZERO(&except_fds);
					FD_SET(sock, &write_fds);
					FD_SET(sock, &except_fds);
					delay.tv_sec  = 0; /* Poll. */
					delay.tv_usec = 0;
					/* In Windows failure is indicated by the bit being set in
					   the exception set rather than the write set. */
					sel = select(FD_SETSIZE,&read_fds,&write_fds,&except_fds,&delay);
					if (sel < 0)
					{
						int err = GETERROR;
						if (err != EINTR)
							raise_syscall("select failed", err);
						/* else continue */
					}
					else if (sel == 0)
					{
						/* Nothing yet */
						block_and_restart(-1, 0, POLY_SYS_network);
							/* -1 => not for reading. */
					}
					else /* Definite result. */
					{
						int result = 0;
						int len = sizeof(result);
						strm->ioBits &= ~IO_BIT_INPROGRESS; /* No longer in progress. */
						if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char*)&result, &len) != 0
							|| result != 0)
							raise_syscall("connect failed", MAPERROR(result));
						return Make_arbitrary_precision(0); /* Success. */
					}
				}
				else
				{
					int err;
					res = connect(strm->device.sock, psock, psAddr->length);
					if (res == 0) return Make_arbitrary_precision(0); /* OK */
					/* It isn't clear that EINTR can ever occur with
					   connect, but just to be safe, we retry. */
					err = GETERROR;
					if ((err == EWOULDBLOCK || err == EINPROGRESS) && c == 48 /*blocking version*/)
					{
						strm->ioBits |= IO_BIT_INPROGRESS;
						block_and_restart(-1, 0, POLY_SYS_network);
							/* -1 => not for reading. */
					}
					else if (err != EINTR)
						raise_syscall("connect failed", err);
					/* else try again. */
				}
			}
		}

	case 49: /* Put socket into listening mode. */
		{
			PIOSTRUCT strm = get_stream((Handle)&DEREFHANDLE(args)[0]);
			int backlog = get_C_ulong(DEREFHANDLE(args)[1]);
			if (strm == NULL) raise_syscall("Stream is closed", EBADF);
			if (listen(strm->device.sock, backlog) != 0)
				raise_syscall("listen failed", GETERROR);
			return Make_arbitrary_precision(0);
		}

	case 50: /* Shutdown the socket. */
		{
			PIOSTRUCT strm = get_stream((Handle)&DEREFHANDLE(args)[0]);
			int mode = 0;
			if (strm == NULL) raise_syscall("Stream is closed", EBADF);
			switch (get_C_ulong(DEREFHANDLE(args)[1]))
			{
			case 1: mode = SHUT_RD; break;
			case 2: mode = SHUT_WR; break;
			case 3: mode = SHUT_RDWR;
			}
			if (shutdown(strm->device.sock, mode) != 0)
				raise_syscall("shutdown failed", GETERROR);
			return Make_arbitrary_precision(0);
		}

	case 51: /* Send data on a socket. */
    case 60: /* Non-blocking send. */
		{
			PIOSTRUCT strm = get_stream((Handle)&DEREFHANDLE(args)[0]);
			char *base = (char*)DEREFHANDLE(args)[1];
			char	ch;
			unsigned int offset = get_C_ulong(DEREFHANDLE(args)[2]);
			unsigned int length = get_C_ulong(DEREFHANDLE(args)[3]);
			unsigned int dontRoute = get_C_ulong(DEREFHANDLE(args)[4]);
			unsigned int outOfBand = get_C_ulong(DEREFHANDLE(args)[5]);
			int flags = 0, sent;
			if (dontRoute != 0) flags |= MSG_DONTROUTE;
			if (outOfBand != 0) flags |= MSG_OOB;
			if (strm == NULL) raise_syscall("Stream is closed", EBADF);
			if (IS_INT(base)) {
				/* Handle the special case where we are sending a single
				   byte vector and the "address" is the tagged byte itself. */
				ch = UNTAGGED(base);
				base = &ch;
				offset = 0;
				length = 1;
			}

			while (1)
			{
				int err;
				sent = send(strm->device.sock, base+offset, length, flags);
				/* It isn't clear that EINTR can ever occur with
				   send but just to be safe we deal with that case and
				   retry the send. */
				if (sent != SOCKET_ERROR) /* OK. */
					return Make_arbitrary_precision(sent);
				err = GETERROR;
				if (err == EWOULDBLOCK && c == 51 /* blocking */)
				{
					block_and_restart(-1, 0, POLY_SYS_network);
						/* -1 => not for reading */
					assert(0); /* Must not have returned. */
				}
				else if (err != EINTR)
					raise_syscall("send failed", err);
				/* else try again */
			}
		}

	case 52: /* Send data on a socket to a given address. */
    case 61: /* Non-blocking send. */
		{
			PIOSTRUCT strm = get_stream((Handle)&DEREFHANDLE(args)[0]);
			pstring psAddr = (pstring)DEREFHANDLE(args)[1];
			char *base = (char*)DEREFHANDLE(args)[2];
			char	ch;
			unsigned int offset = get_C_ulong(DEREFHANDLE(args)[3]);
			unsigned int length = get_C_ulong(DEREFHANDLE(args)[4]);
			unsigned int dontRoute = get_C_ulong(DEREFHANDLE(args)[5]);
			unsigned int outOfBand = get_C_ulong(DEREFHANDLE(args)[6]);
			int flags = 0, sent;
			if (dontRoute != 0) flags |= MSG_DONTROUTE;
			if (outOfBand != 0) flags |= MSG_OOB;
			if (strm == NULL) raise_syscall("Stream is closed", EBADF);
			if (IS_INT(base)) {
				/* Handle the special case where we are sending a single
				   byte vector and the "address" is the tagged byte itself. */
				ch = UNTAGGED(base);
				base = &ch;
				offset = 0;
				length = 1;
			}

			while (1)
			{
				int err;
				sent = sendto(strm->device.sock, base+offset, length, flags,
							(struct sockaddr *)psAddr->chars, psAddr->length);
				/* It isn't clear that EINTR can ever occur with
				   send but just to be safe we deal with that case and
				   retry the send. */
				if (sent != SOCKET_ERROR) /* OK. */
					return Make_arbitrary_precision(sent);
				err = GETERROR;
				if (err == EWOULDBLOCK && c == 52 /* blocking */)
				{
					block_and_restart(-1, 0, POLY_SYS_network);
					assert(0); /* Must not have returned. */
				}
				else if (err != EINTR)
					raise_syscall("sendto failed", err);
				/* else try again */
			}
		}

	case 53: /* Receive data into an array. */
    case 62: /* Non-blocking receive. */
		{
			PIOSTRUCT strm = get_stream((Handle)&DEREFHANDLE(args)[0]);
			char *base = (char*)DEREFHANDLE(args)[1];
			unsigned int offset = get_C_ulong(DEREFHANDLE(args)[2]);
			unsigned int length = get_C_ulong(DEREFHANDLE(args)[3]);
			unsigned int peek = get_C_ulong(DEREFHANDLE(args)[4]);
			unsigned int outOfBand = get_C_ulong(DEREFHANDLE(args)[5]);
			int flags = 0, recvd;
			int inDatabase = IS_LOCAL_MUTABLE(base) ? 0: 1;
			if (peek != 0) flags |= MSG_PEEK;
			if (outOfBand != 0) flags |= MSG_OOB;
			if (strm == NULL) raise_syscall("Stream is closed", EBADF);

			while (1) {
				int err;
				if (inDatabase) RemoveObjectProtection(H, base);
				recvd = recv(strm->device.sock, base+offset, length, flags);
				err = GETERROR;
				if (recvd != SOCKET_ERROR) { /* OK. */
					/* It appears that recv may return the length of the
					   message if that is longer than the buffer. */
					if (recvd > (int)length) recvd = length;
					return Make_arbitrary_precision(recvd);
				}
				if (err == EWOULDBLOCK && c == 53 /* blocking */)
				{
					/* Block until something arrives. */
					block_and_restart(outOfBand ? -1 : strm->device.sock,
						0, POLY_SYS_network);
					assert(0); /* Must not have returned. */
				}
				else if (err != EINTR)
					raise_syscall("recv failed", err);
				/* else try again */
			}
		}

	case 54: /* Receive data into an array and return the sender's
				address along with the length.  In Windows this can
				only be used with datagrams. */
    case 63: /* Non-blocking receive. */
		{
			PIOSTRUCT strm = get_stream((Handle)&DEREFHANDLE(args)[0]);
			char *base = (char*)DEREFHANDLE(args)[1];
			unsigned int offset = get_C_ulong(DEREFHANDLE(args)[2]);
			unsigned int length = get_C_ulong(DEREFHANDLE(args)[3]);
			unsigned int peek = get_C_ulong(DEREFHANDLE(args)[4]);
			unsigned int outOfBand = get_C_ulong(DEREFHANDLE(args)[5]);
			int flags = 0, recvd, addrLen;
			struct sockaddr resultAddr;
			int inDatabase = IS_LOCAL_MUTABLE(base) ? 0: 1;

			if (peek != 0) flags |= MSG_PEEK;
			if (outOfBand != 0) flags |= MSG_OOB;
			if (strm == NULL) raise_syscall("Stream is closed", EBADF);

			while (1) {
				int err;
				if (inDatabase) RemoveObjectProtection(H, base);

				recvd = recvfrom(strm->device.sock, base+offset,
								 length, flags, &resultAddr, &addrLen);
				err = GETERROR;

				if (recvd != SOCKET_ERROR) { /* OK. */
					Handle addrHandle, lengthHandle, pair;
					if (recvd > (int)length) recvd = length;
					lengthHandle = Make_arbitrary_precision(recvd);
					addrHandle = SAVE(Buffer_to_Poly((char*)&resultAddr, addrLen));
					pair = ALLOC(2);
					DEREFHANDLE(pair)[0] = DEREFWORDHANDLE(lengthHandle);
					DEREFHANDLE(pair)[1] = DEREFWORDHANDLE(addrHandle);
					return pair;
				}
				if (err == EWOULDBLOCK && c == 54 /* blocking */)
				{
					block_and_restart(outOfBand ? -1 : strm->device.sock,
						0, POLY_SYS_network);
					assert(0); /* Must not have returned. */
				}
				else if (err != EINTR)
					raise_syscall("recvfrom failed", err);
				/* else try again */
			}
		}

	case 55: /* Create a socket pair. */
#ifdef WINDOWS_PC
		/* Not implemented. */
		raise_syscall("socketpair not implemented", -WSAEAFNOSUPPORT);
#else
		{
			Handle str_token1 = make_stream_entry();
			Handle str_token2 = make_stream_entry();
			Handle pair;
			PIOSTRUCT strm1, strm2;
			int stream_no1 = STREAMID(str_token1);
			int stream_no2 = STREAMID(str_token2);
			int af = get_C_long(DEREFHANDLE(args)[0]);
			int type = get_C_long(DEREFHANDLE(args)[1]);
			int proto = get_C_long(DEREFHANDLE(args)[2]);
			int onOff = 1;
			SOCKET skt[2];
			if (socketpair(af, type, proto, skt) != 0)
			{
				free_stream_entry(stream_no1);
				free_stream_entry(stream_no2);
				switch (GETERROR)
				{
				case EMFILE: /* too many open files */
					{
						if (emfileFlag) /* Previously had an EMFILE error. */
							raise_syscall("socket failed", EMFILE);
						emfileFlag = 1;
						FullGC(); /* May clear emfileFlag if we close a file. */
						goto TryAgain;
					}
				case EINTR: goto TryAgain;
				default: raise_syscall("socketpair failed", GETERROR);
				}
			}
			/* Set the sockets to non-blocking mode. */
			if (proper_ioctl(skt[0], FIONBIO, &onOff) < 0 ||
				proper_ioctl(skt[1], FIONBIO, &onOff) < 0)
			{
				free_stream_entry(stream_no1);
				free_stream_entry(stream_no2);
				proper_close(skt[0]);
				proper_close(skt[1]);
				raise_syscall("ioctl failed", GETERROR);
			}
			strm1 = &basic_io_vector[stream_no1];
			strm1->device.sock = skt[0];
			strm1->ioBits =
				IO_BIT_OPEN | IO_BIT_READ | IO_BIT_WRITE | IO_BIT_SOCKET ;
			strm2 = &basic_io_vector[stream_no2];
			strm2->device.sock = skt[1];
			strm2->ioBits =
				IO_BIT_OPEN | IO_BIT_READ | IO_BIT_WRITE | IO_BIT_SOCKET ;
			/* Return the two streams as a pair. */
			pair = ALLOC(2);
			DEREFHANDLE(pair)[0] = DEREFWORDHANDLE(str_token1);
			DEREFHANDLE(pair)[1] = DEREFWORDHANDLE(str_token2);
			return pair;
 		}
#endif

	case 56: /* Create a Unix socket address from a string. */
#ifdef WINDOWS_PC
		/* Not implemented. */
		raise_syscall("Unix addresses not implemented", -WSAEAFNOSUPPORT);
#else
		{
			struct sockaddr_un addr;
			int length;
			memset(&addr, 0, sizeof(addr));
			addr.sun_family = AF_UNIX;
#ifdef FREEBSD
			addr.sun_len = sizeof(addr);
#endif
			length = Poly_string_to_C(DEREFSTRINGHANDLE(args),
						addr.sun_path, sizeof(addr.sun_path));
			if (length > sizeof(addr.sun_path))
				raise_syscall("Address too long", ENAMETOOLONG);
			return SAVE(Buffer_to_Poly((char*)&addr, sizeof(addr)));
		}
#endif

	case 57: /* Get the file name from a Unix socket address. */
#ifdef WINDOWS_PC
		/* Not implemented. */
		raise_syscall("Unix addresses not implemented", -WSAEAFNOSUPPORT);
#else
		{
			pstring psAddr = DEREFSTRINGHANDLE(args);
			struct sockaddr_un *psock = (struct sockaddr_un *)&psAddr->chars;
			return SAVE(C_string_to_Poly(psock->sun_path));
		}
#endif

    case 64: /* Blocking select call. Infinite timeout. */
        return selectCall(args, 1);

    case 65: /* Polling select call. Zero timeout. */
        return selectCall(args, 2);

    case 66: /* Select call with non-zero timeout. */
        return selectCall(args, 0);


	default: crash("Unknown net function: %d\n", c);
	}
}

/* "Polymorphic" function to generate a list. */
static Handle makeList(int count, char *p, int size, void *arg,
					   Handle (mkEntry)(void*, char*))
{
	Handle list = SAVE(nil_value);
	Handle saved = mark_save_vec();
	/* Start from the end of the list. */
	p += count*size;
	while (count > 0)
	{
		Handle value, next;
		p -= size; /* Back up to the last entry. */
		value = mkEntry(arg, p);
		next  = ALLOC(SIZEOF(ML_Cons_Cell));

		DEREFLISTHANDLE(next)->h = DEREFWORDHANDLE(value); 
		DEREFLISTHANDLE(next)->t = DEREFLISTHANDLE(list);

		DEREFHANDLE(list) = DEREFHANDLE(next);
		reset_save_vec(saved);
		count--;
	}
	return list;
}

static Handle mkAddr(void *arg, char *p)
{
	int j;
	struct hostent *host = (struct hostent *)arg;
	unsigned long addr = 0;
	/* Addresses are in network order so this is fairly easy.
	   In practice they will be 4 byte entries so we could
	   just use ntohl. */
	for (j = 0; j < host->h_length; j++)
		addr = (addr << 8) | ((*(char**)p)[j] & 255);
	return Make_unsigned(addr);
}

/* Convert a host entry into a tuple for ML. */
static Handle makeHostEntry(struct hostent *host)
{
	/* We need to do all this in the right order.  We cannot
	   construct the result tuple until all the values are
	   ready.  We have to save each entry on the save stack
	   just in case of a garbage collection. */
	int i;
	char **p;
	Handle aliases, name, addrType, result;
	Handle addrList = SAVE(nil_value);

	/* Canonical name. */
	name = SAVE(C_string_to_Poly(host->h_name));

	/* Aliases. */
	for (i=0, p = host->h_aliases; *p != NULL; p++, i++);
	aliases = convert_string_list(i, host->h_aliases);

	/* Address type. */
	addrType = Make_unsigned(host->h_addrtype);

	/* Addresses. */
	/* Count them first and then work from the end back. */
	for (i=0, p = host->h_addr_list; *p != NULL; p++, i++);
	addrList = makeList(i, (char*)host->h_addr_list, sizeof(char*), host, mkAddr);

	/* Make the result structure. */
	result = ALLOC(4);
	DEREFHANDLE(result)[0] = DEREFWORDHANDLE(name);
	DEREFHANDLE(result)[1] = DEREFWORDHANDLE(aliases);
	DEREFHANDLE(result)[2] = DEREFWORDHANDLE(addrType);
	DEREFHANDLE(result)[3] = DEREFWORDHANDLE(addrList);
	return result;
}

static Handle makeProtoEntry(struct protoent *proto)
{
	int i;
	char **p;
	Handle aliases, name, protocol, result;

	/* Canonical name. */
	name = SAVE(C_string_to_Poly(proto->p_name));

	/* Aliases. */
	for (i=0, p = proto->p_aliases; *p != NULL; p++, i++);
	aliases = convert_string_list(i, proto->p_aliases);

	/* Protocol number. */
	protocol = Make_unsigned(proto->p_proto);

	/* Make the result structure. */
	result = ALLOC(3);
	DEREFHANDLE(result)[0] = DEREFWORDHANDLE(name);
	DEREFHANDLE(result)[1] = DEREFWORDHANDLE(aliases);
	DEREFHANDLE(result)[2] = DEREFWORDHANDLE(protocol);
	return result;
}

static Handle makeServEntry(struct servent *serv)
{
	int i;
	char **p;
	Handle aliases, name, protocol, result, port;

	/* Canonical name. */
	name = SAVE(C_string_to_Poly(serv->s_name));

	/* Aliases. */
	for (i=0, p = serv->s_aliases; *p != NULL; p++, i++);
	aliases = convert_string_list(i, serv->s_aliases);

	/* Port number. */
	port = Make_unsigned(ntohs(serv->s_port));

	/* Protocol name. */
	protocol = SAVE(C_string_to_Poly(serv->s_proto));

	/* Make the result structure. */
	result = ALLOC(4);
	DEREFHANDLE(result)[0] = DEREFWORDHANDLE(name);
	DEREFHANDLE(result)[1] = DEREFWORDHANDLE(aliases);
	DEREFHANDLE(result)[2] = DEREFWORDHANDLE(port);
	DEREFHANDLE(result)[3] = DEREFWORDHANDLE(protocol);
	return result;
}

static Handle makeNetEntry(struct netent *net)
{
	int i;
	char **p;
	Handle aliases, name, addrType, result, network;

	/* Canonical name. */
	name = SAVE(C_string_to_Poly(net->n_name));

	/* Aliases. */
	for (i=0, p = net->n_aliases; *p != NULL; p++, i++);
	aliases = convert_string_list(i, net->n_aliases);

	/* Address type. */
	addrType = Make_unsigned(net->n_addrtype);

	/* Protocol name. */
	network = Make_unsigned(ntohl(net->n_net));

	/* Make the result structure. */
	result = ALLOC(4);
	DEREFHANDLE(result)[0] = DEREFWORDHANDLE(name);
	DEREFHANDLE(result)[1] = DEREFWORDHANDLE(aliases);
	DEREFHANDLE(result)[2] = DEREFWORDHANDLE(addrType);
	DEREFHANDLE(result)[3] = DEREFWORDHANDLE(network);
	return result;
}

static Handle mkAftab(void *arg, char *p)
{
	struct af_tab_struct *af = (struct af_tab_struct *)p;
	Handle result, name, num;
	/* Construct a pair of the string and the number. */
	name = SAVE(C_string_to_Poly(af->af_name));
	num = Make_unsigned(af->af_num);
	result = ALLOC(2);
	DEREFHANDLE(result)[0] = DEREFWORDHANDLE(name);
	DEREFHANDLE(result)[1] = DEREFWORDHANDLE(num);
	return result;
}

static Handle mkSktab(void *arg, char *p)
{
	struct sk_tab_struct *sk = (struct sk_tab_struct *)p;
	Handle result, name, num;
	/* Construct a pair of the string and the number. */
	name = SAVE(C_string_to_Poly(sk->sk_name));
	num = Make_unsigned(sk->sk_num);
	result = ALLOC(2);
	DEREFHANDLE(result)[0] = DEREFWORDHANDLE(name);
	DEREFHANDLE(result)[1] = DEREFWORDHANDLE(num);
	return result;
}

/* This sets an option and can also be used to set an integer. */
static Handle setSocketOption(Handle args, int level, int opt)
{
	PIOSTRUCT strm = get_stream((Handle)&DEREFHANDLE(args)[0]);
	int onOff = get_C_long(DEREFHANDLE(args)[1]);
	if (strm == NULL) raise_syscall("Stream is closed", EBADF);
	if (setsockopt(strm->device.sock, level, opt,
		(char*)&onOff, sizeof(int)) != 0)
		raise_syscall("setsockopt failed", GETERROR);
	return Make_arbitrary_precision(0);
}

/* Get a socket option as a boolean */
static Handle getSocketOption(Handle args, int level, int opt)
{
	PIOSTRUCT strm = get_stream(args);
	int onOff = 0, size = sizeof(int);
	if (strm == NULL) raise_syscall("Stream is closed", EBADF);
	if (getsockopt(strm->device.sock, level, opt,
		(char*)&onOff, &size) != 0)
		raise_syscall("getsockopt failed", GETERROR);
	return Make_arbitrary_precision(onOff == 0 ? 0 : 1);
}

/* Get a socket option as an integer */
static Handle getSocketInt(Handle args, int level, int opt)
{
	PIOSTRUCT strm = get_stream(args);
	int optVal = 0, size = sizeof(int);
	if (strm == NULL) raise_syscall("Stream is closed", EBADF);
	if (getsockopt(strm->device.sock, level, opt,
		(char*)&optVal, &size) != 0)
		raise_syscall("getsockopt failed", GETERROR);
	return Make_arbitrary_precision(optVal);
}

/* Helper function for selectCall.  Creates the result vector of active sockets. */
static Handle getSelectResult(Handle args, int offset, fd_set *pFds)
{
    /* Construct the result vectors. */
    word *inVec = (word*)DEREFHANDLE(args)[offset];
    int nVec = OBJECT_LENGTH(inVec);
    int nRes = 0;
    int i;
    for (i = 0; i < nVec; i++) {
        PIOSTRUCT strm = get_stream((Handle)&inVec[i]);
        if (FD_ISSET(strm->device.sock, pFds)) nRes++;
    }
    if (nRes == 0) return (Handle)(&EMPTYSTRING); /* None - return empty vector. */
    else {
        Handle result = ALLOC(nRes);
        inVec = (word*)DEREFHANDLE(args)[offset]; /* It could have moved as a result of a gc. */
        nRes = 0;
        for (i = 0; i < nVec; i++) {
            PIOSTRUCT strm = get_stream((Handle)&inVec[i]);
            if (FD_ISSET(strm->device.sock, pFds))
                (DEREFWORDHANDLE(result))[nRes++] = inVec[i];
        }
        return result;
    }
}

/* Wrapper for "select" call.  The arguments are arrays of socket ids.  These arrays are
   updated so that "active" sockets are left unchanged and inactive sockets are set to
   minus one.  */
static Handle selectCall(Handle args, int blockType)
{
    fd_set readers, writers, excepts;
    struct timeval timeout;
    int i, nVec, selectRes;
	Handle rdResult, wrResult, exResult, result;
    /* Set up the bitmaps for the select call from the arrays. */
    word *readVec = (word*)DEREFHANDLE(args)[0];
    word *writeVec = (word*)DEREFHANDLE(args)[1];
    word *excVec = (word*)DEREFHANDLE(args)[2];
    FD_ZERO(&readers);
    FD_ZERO(&writers);
    FD_ZERO(&excepts);
    nVec = OBJECT_LENGTH(readVec);
    for (i = 0; i < nVec; i++) {
        PIOSTRUCT strm = get_stream((Handle)&readVec[i]);
        if (strm == NULL) raise_syscall("Stream is closed", EBADF); 
        FD_SET(strm->device.sock, &readers);
    }
    nVec = OBJECT_LENGTH(writeVec);
    for (i = 0; i < nVec; i++) {
        PIOSTRUCT strm = get_stream((Handle)&writeVec[i]);
        if (strm == NULL) raise_syscall("Stream is closed", EBADF); 
        FD_SET(strm->device.sock, &writers);
    }
    nVec = OBJECT_LENGTH(excVec);
    for (i = 0; i < nVec; i++) {
        PIOSTRUCT strm = get_stream((Handle)&excVec[i]);
        if (strm == NULL) raise_syscall("Stream is closed", EBADF); 
        FD_SET(strm->device.sock, &excepts);
    }
    /* Whatever the timeout specified we simply poll here. */
    memset(&timeout, 0, sizeof(timeout));
    selectRes = select(FD_SETSIZE, &readers, &writers, &excepts, &timeout);
    if (selectRes < 0) raise_syscall("select failed", GETERROR);

    if (selectRes == 0) { /* Timed out.  Have to look at the timeout value. */
		switch (blockType)
		{
		case 0: /* Check the timeout. */
			{
			/* The time argument is an absolute time. */
#ifdef WINDOWS_PC
			FILETIME ftTime, ftNow;
			/* Get the file time. */
			get_C_pair(DEREFHANDLE(args)[3],
				&ftTime.dwHighDateTime, &ftTime.dwLowDateTime);
			GetSystemTimeAsFileTime(&ftNow);
			/* If the timeout time is earlier than the current time
			   we must return, otherwise we block. */
			if (CompareFileTime(&ftTime, &ftNow) <= 0)
				break; /* Return the empty set. */
			/* else drop through and block. */
#else /* Unix */
			struct timeval tv;
			struct timezone tz;
			/* We have a value in microseconds.  We need to split
			   it into seconds and microseconds. */
			Handle hTime = SAVE(DEREFWORDHANDLE(args)[3]);
			Handle hMillion = Make_arbitrary_precision(1000000);
			unsigned secs =
				get_C_ulong(DEREFWORDHANDLE(div_longc(hMillion, hTime)));
			unsigned usecs =
				get_C_ulong(DEREFWORDHANDLE(rem_longc(hMillion, hTime)));
			/* If the timeout time is earlier than the current time
			   we must return, otherwise we block. */
			if (gettimeofday(&tv, &tz) != 0)
				raise_syscall("gettimeofday failed", errno);
			if (tv.tv_sec > secs ||
				(tv.tv_sec == secs && tv.tv_usec >= usecs))
				break;
			/* else block. */
#endif
		}
		case 1: /* Block until one of the descriptors is ready. */
			block_and_restart(-1, 0, POLY_SYS_network);
			/*NOTREACHED*/
		case 2: /* Just a simple poll - drop through. */
			break;
		}
    }

    /* Construct the result vectors. */
    rdResult = getSelectResult(args, 0, &readers);
    wrResult = getSelectResult(args, 1, &writers);
    exResult = getSelectResult(args, 2, &excepts);

    result = ALLOC(3);
	DEREFHANDLE(result)[0] = DEREFWORDHANDLE(rdResult);
	DEREFHANDLE(result)[1] = DEREFWORDHANDLE(wrResult);
	DEREFHANDLE(result)[2] = DEREFWORDHANDLE(exResult);
    return result;
}


void uninit_net_system(void)
{
#ifdef WINDOWS_PC
	if (winsock_init) WSACleanup();
	winsock_init = 0;
#endif
}

void init_net_system(void)
{
#ifdef WINDOWS_PC
#ifdef USEWINSOCK2
#define WINSOCK_MAJOR_VERSION	2
#define WINSOCK_MINOR_VERSION	2
#else
#define WINSOCK_MAJOR_VERSION	1
#define WINSOCK_MINOR_VERSION	1
#endif
	WSADATA wsaData;
	WORD wVersion = MAKEWORD(WINSOCK_MINOR_VERSION, WINSOCK_MAJOR_VERSION);
	/* Initialise the system and check that the version it supplied
	   is the one we requested. */
	if(WSAStartup(wVersion, &wsaData) == 0)
	{
		if (wsaData.wVersion == wVersion)
			winsock_init = 1;
		else WSACleanup();
	}
#endif
}

void re_init_net_system(void)
{
}

