diff -urN curl-7.12.2-orig/lib/connect.c curl-7.12.2/lib/connect.c
--- curl-7.12.2-orig/lib/connect.c	2004-10-17 17:21:01.000000000 -0500
+++ curl-7.12.2/lib/connect.c	2004-11-16 15:06:41.000000000 -0600
@@ -97,6 +97,7 @@
 #include "strerror.h"
 #include "connect.h"
 #include "memory.h"
+#include "select.h"
 
 /* The last #include file should be: */
 #include "memdebug.h"
@@ -201,9 +202,6 @@
 int waitconnect(curl_socket_t sockfd, /* socket */
                 long timeout_msec)
 {
-  fd_set fd;
-  fd_set errfd;
-  struct timeval interval;
   int rc;
 #ifdef mpeix
   /* Call this function once now, and ignore the results. We do this to
@@ -213,18 +211,7 @@
 #endif
 
   /* now select() until we get connect or timeout */
-  FD_ZERO(&fd);
-  FD_SET(sockfd, &fd);
-
-  FD_ZERO(&errfd);
-  FD_SET(sockfd, &errfd);
-
-  interval.tv_sec = (int)(timeout_msec/1000);
-  timeout_msec -= interval.tv_sec*1000;
-
-  interval.tv_usec = timeout_msec*1000;
-
-  rc = select(sockfd+1, NULL, &fd, &errfd, &interval);
+  rc = Curl_select(CURL_SOCKET_BAD, sockfd, timeout_msec);
   if(-1 == rc)
     /* error, no connect here, try next */
     return WAITCONN_SELECT_ERROR;
@@ -233,7 +220,7 @@
     /* timeout, no connect today */
     return WAITCONN_TIMEOUT;
 
-  if(FD_ISSET(sockfd, &errfd))
+  if(rc & CSELECT_ERR)
     /* error condition caught */
     return WAITCONN_FDSET_ERROR;
 
diff -urN curl-7.12.2-orig/lib/ftp.c curl-7.12.2/lib/ftp.c
--- curl-7.12.2-orig/lib/ftp.c	2004-10-17 17:21:02.000000000 -0500
+++ curl-7.12.2/lib/ftp.c	2004-11-16 19:10:58.000000000 -0600
@@ -34,9 +34,6 @@
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
-#ifdef HAVE_SYS_SELECT_H
-#include <sys/select.h>
-#endif
 
 #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
 
@@ -94,6 +91,7 @@
 #include "strerror.h"
 #include "memory.h"
 #include "inet_ntop.h"
+#include "select.h"
 
 #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
 #include "inet_ntoa_r.h"
@@ -160,8 +158,7 @@
  */
 static CURLcode AllowServerConnect(struct connectdata *conn)
 {
-  fd_set rdset;
-  struct timeval dt;
+  int timeout_ms;
   struct SessionHandle *data = conn->data;
   curl_socket_t sock = conn->sock[SECONDARYSOCKET];
   struct timeval now = Curl_tvnow();
@@ -169,10 +166,6 @@
   long timeout = data->set.connecttimeout?data->set.connecttimeout:
     (data->set.timeout?data->set.timeout: 0);
 
-  FD_ZERO(&rdset);
-
-  FD_SET(sock, &rdset);
-
   if(timeout) {
     timeout -= timespent;
     if(timeout<=0) {
@@ -182,10 +175,9 @@
   }
 
   /* we give the server 60 seconds to connect to us, or a custom timeout */
-  dt.tv_sec = (int)(timeout?timeout:60);
-  dt.tv_usec = 0;
+  timeout_ms = (timeout?timeout:60) * 1000;
 
-  switch (select(sock+1, &rdset, NULL, NULL, &dt)) {
+  switch (Curl_select(sock, CURL_SOCKET_BAD, timeout_ms)) {
   case -1: /* error */
     /* let's die here */
     failf(data, "Error while waiting for server connect");
@@ -248,9 +240,7 @@
   ssize_t gotbytes;
   char *ptr;
   long timeout;              /* timeout in seconds */
-  struct timeval interval;
-  fd_set rkeepfd;
-  fd_set readfd;
+  int interval_ms;
   struct SessionHandle *data = conn->data;
   char *line_start;
   int code=0; /* default ftp "error code" to return */
@@ -262,13 +252,6 @@
   if (ftpcode)
     *ftpcode = 0; /* 0 for errors */
 
-  FD_ZERO (&readfd);            /* clear it */
-  FD_SET (sockfd, &readfd);     /* read socket */
-
-  /* get this in a backup variable to be able to restore it on each lap in the
-     select() loop */
-  rkeepfd = readfd;
-
   ptr=buf;
   line_start = buf;
 
@@ -302,11 +285,9 @@
     }
 
     if(!ftp->cache) {
-      readfd = rkeepfd;            /* set every lap */
-      interval.tv_sec = 1; /* use 1 second timeout intervals */
-      interval.tv_usec = 0;
+      interval_ms = 1 * 1000;  /* use 1 second timeout intervals */
 
-      switch (select (sockfd+1, &readfd, NULL, NULL, &interval)) {
+      switch (Curl_select(sockfd, CURL_SOCKET_BAD, interval_ms)) {
       case -1: /* select() error, stop reading */
         result = CURLE_RECV_ERROR;
         failf(data, "FTP response aborted due to select() error: %d", errno);
diff -urN curl-7.12.2-orig/lib/http.c curl-7.12.2/lib/http.c
--- curl-7.12.2-orig/lib/http.c	2004-10-08 03:06:16.000000000 -0500
+++ curl-7.12.2/lib/http.c	2004-11-16 19:09:45.000000000 -0600
@@ -70,10 +70,6 @@
 #include <sys/param.h>
 #endif
 
-#ifdef HAVE_SYS_SELECT_H
-#include <sys/select.h>
-#endif
-
 #endif
 
 #include "urldata.h"
@@ -94,6 +90,7 @@
 #include "hostip.h"
 #include "http.h"
 #include "memory.h"
+#include "select.h"
 
 #define _MPRINTF_REPLACE /* use our functions only */
 #include <curl/mprintf.h>
@@ -917,9 +914,7 @@
   ssize_t gotbytes;
   char *ptr;
   long timeout = 3600; /* default timeout in seconds */
-  struct timeval interval;
-  fd_set rkeepfd;
-  fd_set readfd;
+  int interval_ms;
   char *line_start;
   char *host_port;
   curl_socket_t tunnelsocket = conn->sock[sockindex];
@@ -967,13 +962,6 @@
     if(result)
       return result;
 
-    FD_ZERO (&readfd);          /* clear it */
-    FD_SET (tunnelsocket, &readfd);     /* read socket */
-
-    /* get this in a backup variable to be able to restore it on each lap in
-       the select() loop */
-    rkeepfd = readfd;
-
     ptr=data->state.buffer;
     line_start = ptr;
 
@@ -982,9 +970,7 @@
     keepon=TRUE;
 
     while((nread<BUFSIZE) && (keepon && !error)) {
-      readfd = rkeepfd;     /* set every lap */
-      interval.tv_sec = 1;  /* timeout each second and check the timeout */
-      interval.tv_usec = 0;
+      interval_ms = 1;  /* timeout each second and check the timeout */
 
       if(data->set.timeout) {
         /* if timeout is requested, find out how much remaining time we have */
@@ -997,7 +983,7 @@
         }
       }
 
-      switch (select (tunnelsocket+1, &readfd, NULL, NULL, &interval)) {
+      switch (Curl_select(tunnelsocket, CURL_SOCKET_BAD, interval_ms)) {
       case -1: /* select() error, stop reading */
         error = SELECT_ERROR;
         failf(data, "Proxy CONNECT aborted due to select() error");
diff -urN curl-7.12.2-orig/lib/Makefile.inc curl-7.12.2/lib/Makefile.inc
--- curl-7.12.2-orig/lib/Makefile.inc	2004-09-16 06:13:37.000000000 -0500
+++ curl-7.12.2/lib/Makefile.inc	2004-11-16 13:36:51.000000000 -0600
@@ -7,7 +7,8 @@
   memdebug.c http_chunks.c strtok.c connect.c llist.c hash.c multi.c	\
   content_encoding.c share.c http_digest.c md5.c http_negotiate.c	\
   http_ntlm.c inet_pton.c strtoofft.c strerror.c hostares.c hostasyn.c	\
-  hostip4.c hostip6.c hostsyn.c hostthre.c inet_ntop.c parsedate.c
+  hostip4.c hostip6.c hostsyn.c hostthre.c inet_ntop.c parsedate.c	\
+  select.c
 
 HHEADERS = arpa_telnet.h netrc.h file.h timeval.h base64.h hostip.h	\
   progress.h formdata.h cookie.h http.h sendf.h ftp.h url.h dict.h	\
@@ -16,4 +17,4 @@
   http_chunks.h strtok.h connect.h llist.h hash.h content_encoding.h	\
   share.h md5.h http_digest.h http_negotiate.h http_ntlm.h ca-bundle.h	\
   inet_pton.h strtoofft.h strerror.h inet_ntop.h curlx.h memory.h	\
-  setup.h transfer.h
+  setup.h transfer.h select.h
diff -urN curl-7.12.2-orig/lib/select.c curl-7.12.2/lib/select.c
--- curl-7.12.2-orig/lib/select.c	1969-12-31 18:00:00.000000000 -0600
+++ curl-7.12.2/lib/select.c	2004-11-17 12:32:03.000000000 -0600
@@ -0,0 +1,231 @@
+/***************************************************************************
+ *                                  _   _ ____  _     
+ *  Project                     ___| | | |  _ \| |    
+ *                             / __| | | | |_) | |    
+ *                            | (__| |_| |  _ <| |___ 
+ *                             \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ * 
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ * $Id: select.c $
+ ***************************************************************************/
+
+#include "setup.h"
+
+#ifdef HAVE_SYS_SELECT_H
+#include <sys/select.h>
+#endif
+
+#ifndef HAVE_SELECT
+#error "We can't compile without select() support!"
+#endif
+
+#include "select.h"
+
+/*
+ * This is an internal function used for waiting for read or write
+ * events on single file descriptors.  It attempts to replace select()
+ * in order to avoid limits with FD_SETSIZE.
+ *
+ * Return values:
+ *   -1 = system call error
+ *    0 = timeout
+ *    CSELECT_IN | CSELECT_OUT | CSELECT_ERR
+ */
+int Curl_select(int readfd, int writefd, int timeout_ms)
+{
+#ifdef HAVE_POLL_FINE
+  struct pollfd pfd[2];
+  int num;
+  int r;
+  int ret;
+  
+  num = 0;
+  if (readfd != CURL_SOCKET_BAD) {
+    pfd[num].fd = readfd;
+    pfd[num].events = POLLIN;
+    num++;
+  }
+  if (writefd != CURL_SOCKET_BAD) {
+    pfd[num].fd = writefd;
+    pfd[num].events = POLLOUT;
+    num++;
+  }
+
+  r = poll(pfd, num, timeout_ms);
+
+  if (r < 0)
+    return -1;
+  if (r == 0)
+    return 0;
+
+  ret = 0;
+  num = 0;
+  if (readfd != CURL_SOCKET_BAD) {
+    if (pfd[num].revents & POLLIN)
+      ret |= CSELECT_IN;
+    if (pfd[num].revents & POLLERR)
+      ret |= CSELECT_ERR;
+    num++;
+  }
+  if (writefd != CURL_SOCKET_BAD) {
+    if (pfd[num].revents & POLLOUT)
+      ret |= CSELECT_OUT;
+    if (pfd[num].revents & POLLERR)
+      ret |= CSELECT_ERR;
+  }
+  
+  return ret;
+#else
+  struct timeval timeout;
+  fd_set fds_read;
+  fd_set fds_write;
+  fd_set fds_err;
+  int maxfd;
+  int r;
+  int ret;
+  
+  timeout.tv_sec = timeout_ms / 1000;
+  timeout.tv_usec = (timeout_ms % 1000) * 1000;
+
+  FD_ZERO(&fds_err);
+  maxfd = -1;
+
+  FD_ZERO(&fds_read);
+  if (readfd != CURL_SOCKET_BAD) {
+    if ((readfd < 0) || (readfd >= FD_SETSIZE)) {
+      errno = EINVAL;
+      return -1;
+    }
+    FD_SET(readfd, &fds_read);
+    FD_SET(readfd, &fds_err);
+    maxfd = readfd;
+  }
+  
+  FD_ZERO(&fds_write);
+  if (writefd != CURL_SOCKET_BAD) {
+    if ((writefd < 0) || (writefd >= FD_SETSIZE)) {
+      errno = EINVAL;
+      return -1;
+    }
+    FD_SET(writefd, &fds_write);
+    FD_SET(writefd, &fds_err);
+    if (writefd > maxfd)
+      maxfd = writefd;
+  }
+
+  r = select(maxfd + 1, &fds_read, &fds_write, &fds_err, &timeout);
+
+  if (r < 0)
+    return -1;
+  if (r == 0)
+    return 0;
+
+  ret = 0;
+  if (readfd != CURL_SOCKET_BAD) {
+    if (FD_ISSET(readfd, &fds_read))
+      ret |= CSELECT_IN;
+    if (FD_ISSET(readfd, &fds_err))
+      ret |= CSELECT_ERR;
+  }
+  if (writefd != CURL_SOCKET_BAD) {
+    if (FD_ISSET(writefd, &fds_write))
+      ret |= CSELECT_OUT;
+    if (FD_ISSET(writefd, &fds_err))
+      ret |= CSELECT_ERR;
+  }
+
+  return ret;
+#endif
+}
+
+/*
+ * This is a wrapper around poll().  If poll() does not exist, then
+ * select() is used instead.  An error is returned if select() is
+ * being used and a file descriptor too large for FD_SETSIZE.
+ *
+ * Return values:
+ *   -1 = system call error or fd >= FD_SETSIZE
+ *    0 = timeout
+ *    1 = number of structures with non zero revent fields
+ */
+int Curl_poll(struct pollfd ufds[], unsigned int nfds, int timeout_ms)
+{
+#ifdef HAVE_POLL_FINE
+    return poll(ufds, nfds, timeout_ms);
+#else
+  struct timeval timeout;
+  struct timeval *ptimeout;
+  fd_set fds_read;
+  fd_set fds_write;
+  fd_set fds_err;
+  int maxfd;
+  int r;
+  int i;
+
+  FD_ZERO(&fds_read);
+  FD_ZERO(&fds_write);
+  FD_ZERO(&fds_err);
+  maxfd = -1;
+    
+  for (i = 0; i < nfds; i++) {
+    if (ufds[i].fd < 0)
+      continue;
+    if (ufds[i].fd >= FD_SETSIZE)) {
+      errno = EINVAL;
+      return -1;
+    }
+    if (ufds[i].fd > maxfd)
+      maxfd = ufds[i].fd;
+    if (ufds[i].events & POLLIN)
+      FD_SET(ufds[i].fd, &fds_read);
+    if (ufds[i].events & POLLOUT)
+      FD_SET(ufds[i].fd, &fds_write);
+    if (ufds[i].events & POLLERR)
+      FD_SET(ufds[i].fd, &fds_err);
+  }
+
+  if (timeout_ms < 0) {
+    ptimeout = NULL;      /* wait forever */
+  } else {
+    timeout.tv_sec = timeout_ms / 1000;
+    timeout.tv_usec = (timeout_ms % 1000) * 1000;
+    ptimeout = timeout;
+  }
+
+  r = select(maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout);
+  
+  if (r < 0)
+    return -1;
+  if (r == 0)
+    return 0;
+
+  r = 0;
+  for (i = 0; i < nfds; i++) {
+    ufds[i].revents = 0;
+    if (ufds[i].fd < 0)
+      continue;
+    if (FD_ISSET(ufds[i].fd, &fds_read))
+      ufds[i].revents |= POLLIN;
+    if (FD_ISSET(ufds[i].fd, &fds_write))
+      ufds[i].revents |= POLLOUT;
+    if (FD_ISSET(ufds[i].fd, &fds_err))
+      ufds[i].revents |= POLLERR;
+    if (ufds[i].revents != 0)
+      r++;
+  }
+
+  return r;
+#endif
+}
diff -urN curl-7.12.2-orig/lib/select.h curl-7.12.2/lib/select.h
--- curl-7.12.2-orig/lib/select.h	1969-12-31 18:00:00.000000000 -0600
+++ curl-7.12.2/lib/select.h	2004-11-17 11:12:51.000000000 -0600
@@ -0,0 +1,57 @@
+#ifndef __SELECT_H
+#define __SELECT_H
+/***************************************************************************
+ *                                  _   _ ____  _     
+ *  Project                     ___| | | |  _ \| |    
+ *                             / __| | | | |_) | |    
+ *                            | (__| |_| |  _ <| |___ 
+ *                             \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ * 
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ * $Id: select.h $
+ ***************************************************************************/
+
+
+#ifdef HAVE_SYS_POLL_H
+#include <sys/poll.h>
+#else
+
+#define POLLIN      0x01
+#define POLLPRI     0x02
+#define POLLOUT     0x04
+#define POLLERR     0x08
+#define POLLHUP     0x10
+#define POLLNVAL    0x20
+
+struct pollfd
+{
+    int     fd;
+    short   events;
+    short   revents;
+};
+
+#endif
+
+
+#define CSELECT_IN   0x01
+#define CSELECT_OUT  0x02
+#define CSELECT_ERR  0x04
+
+int Curl_select(int readfd, int writefd, int timeout_ms);
+
+int Curl_poll(struct pollfd ufds[], unsigned int nfds, int timeout_ms);
+
+
+#endif
diff -urN curl-7.12.2-orig/lib/ssluse.c curl-7.12.2/lib/ssluse.c
--- curl-7.12.2-orig/lib/ssluse.c	2004-10-08 03:06:16.000000000 -0500
+++ curl-7.12.2/lib/ssluse.c	2004-11-16 19:11:36.000000000 -0600
@@ -46,6 +46,7 @@
 #include "ssluse.h"
 #include "connect.h" /* Curl_ourerrno() proto */
 #include "strequal.h"
+#include "select.h"
 
 #define _MPRINTF_REPLACE /* use the internal *printf() functions */
 #include <curl/mprintf.h>
@@ -1260,9 +1261,8 @@
   SSL_set_fd(connssl->handle, sockfd);
 
   while(1) {
-    fd_set writefd;
-    fd_set readfd;
-    struct timeval interval;
+    int writefd;
+    int readfd;
     long timeout_ms;
 
     /* Find out if any timeout is set. If not, use 300 seconds.
@@ -1296,8 +1296,8 @@
       timeout_ms= DEFAULT_CONNECT_TIMEOUT;
 
 
-    FD_ZERO(&writefd);
-    FD_ZERO(&readfd);
+    readfd = CURL_SOCKET_BAD;
+    writefd = CURL_SOCKET_BAD;
 
     err = SSL_connect(connssl->handle);
 
@@ -1308,9 +1308,9 @@
       int detail = SSL_get_error(connssl->handle, err);
 
       if(SSL_ERROR_WANT_READ == detail)
-        FD_SET(sockfd, &readfd);
+        readfd = sockfd;
       else if(SSL_ERROR_WANT_WRITE == detail)
-        FD_SET(sockfd, &writefd);
+        writefd = sockfd;
       else {
         /* untreated error */
         unsigned long errdetail;
@@ -1373,13 +1373,8 @@
       /* we have been connected fine, get out of the connect loop */
       break;
 
-    interval.tv_sec = (int)(timeout_ms/1000);
-    timeout_ms -= interval.tv_sec*1000;
-
-    interval.tv_usec = timeout_ms*1000;
-
     while(1) {
-      what = select(sockfd+1, &readfd, &writefd, NULL, &interval);
+      what = Curl_select(readfd, writefd, timeout_ms);
       if(what > 0)
         /* reabable or writable, go loop in the outer loop */
         break;
diff -urN curl-7.12.2-orig/lib/telnet.c curl-7.12.2/lib/telnet.c
--- curl-7.12.2-orig/lib/telnet.c	2004-10-08 03:06:17.000000000 -0500
+++ curl-7.12.2/lib/telnet.c	2004-11-17 12:29:24.000000000 -0600
@@ -61,10 +61,6 @@
 #include <sys/param.h>
 #endif
 
-#ifdef HAVE_SYS_SELECT_H
-#include <sys/select.h>
-#endif
-
 
 #endif
 
@@ -82,6 +78,7 @@
 
 #include "arpa_telnet.h"
 #include "memory.h"
+#include "select.h"
 
 /* The last #include file should be: */
 #include "memdebug.h"
@@ -1085,8 +1082,8 @@
   DWORD waitret;
   DWORD readfile_read;
 #else
-  fd_set readfd;
-  fd_set keepfd;
+  int interval_ms;
+  struct pollfd pfd[2];
 #endif
   ssize_t nread;
   bool keepon = TRUE;
@@ -1305,27 +1302,21 @@
   if (!FreeLibrary(wsock2))
     infof(data,"FreeLibrary(wsock2) failed (%d)",GetLastError());
 #else
-  FD_ZERO (&readfd);            /* clear it */
-  FD_SET (sockfd, &readfd);
-  FD_SET (0, &readfd);
-
-  keepfd = readfd;
+  pfd[0].fd = sockfd;
+  pfd[0].events = POLLIN;
+  pfd[1].fd = 0;
+  pfd[1].events = POLLIN;
+  interval_ms = 1 * 1000;
 
   while (keepon) {
-    struct timeval interval;
-
-    readfd = keepfd;            /* set this every lap in the loop */
-    interval.tv_sec = 1;
-    interval.tv_usec = 0;
-
-    switch (select (sockfd + 1, &readfd, NULL, NULL, &interval)) {
+    switch (Curl_poll(pfd, 2, interval_ms)) {
     case -1:                    /* error, stop reading */
       keepon = FALSE;
       continue;
     case 0:                     /* timeout */
       break;
     default:                    /* read! */
-      if(FD_ISSET(0, &readfd)) { /* read from stdin */
+      if(pfd[1].revents & POLLIN) { /* read from stdin */
         unsigned char outbuf[2];
         int out_count = 0;
         ssize_t bytes_written;
@@ -1344,7 +1335,7 @@
         }
       }
 
-      if(FD_ISSET(sockfd, &readfd)) {
+      if(pfd[0].revents & POLLIN) {
         /* This OUGHT to check the return code... */
         (void)Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
 
diff -urN curl-7.12.2-orig/lib/transfer.c curl-7.12.2/lib/transfer.c
--- curl-7.12.2-orig/lib/transfer.c	2004-10-08 03:06:17.000000000 -0500
+++ curl-7.12.2/lib/transfer.c	2004-11-17 11:02:57.000000000 -0600
@@ -73,9 +73,6 @@
 #include <sys/select.h>
 #endif
 
-#ifndef HAVE_SELECT
-#error "We can't compile without select() support!"
-#endif
 #ifndef HAVE_SOCKET
 #error "We can't compile without socket() support!"
 #endif
@@ -101,6 +98,7 @@
 #include "http_negotiate.h"
 #include "share.h"
 #include "memory.h"
+#include "select.h"
 
 #define _MPRINTF_REPLACE /* use our functions only */
 #include <curl/mprintf.h>
@@ -116,10 +114,6 @@
   KEEP_WRITE
 };
 
-/* We keep this static and global since this is read-only and NEVER
-   changed. It should just remain a blanked-out timeout value. */
-static struct timeval notimeout={0,0};
-
 /*
  * This function will call the read callback to fill our buffer with data
  * to upload.
@@ -211,43 +205,28 @@
   ssize_t nread; /* number of bytes read */
   int didwhat=0;
 
-  /* These two are used only if no other select() or _fdset() have been
-     invoked before this. This typicly happens if you use the multi interface
-     and call curl_multi_perform() without calling curl_multi_fdset()
-     first. */
-  fd_set extrareadfd;
-  fd_set extrawritefd;
+  int fd_read;
+  int fd_write;
+  int select_res;
 
-  fd_set *readfdp = k->readfdp;
-  fd_set *writefdp = k->writefdp;
   curl_off_t contentlength;
 
-  if((k->keepon & KEEP_READ) && !readfdp) {
-    /* reading is requested, but no socket descriptor pointer was set */
-    FD_ZERO(&extrareadfd);
-    FD_SET(conn->sockfd, &extrareadfd);
-    readfdp = &extrareadfd;
-
-    /* no write, no exceptions, no timeout */
-    select(conn->sockfd+1, readfdp, NULL, NULL, &notimeout);
-  }
-  if((k->keepon & KEEP_WRITE) && !writefdp) {
-    /* writing is requested, but no socket descriptor pointer was set */
-    FD_ZERO(&extrawritefd);
-    FD_SET(conn->writesockfd, &extrawritefd);
-    writefdp = &extrawritefd;
+  if(k->keepon & KEEP_READ)
+    fd_read = conn->sockfd;
+  else
+    fd_read = CURL_SOCKET_BAD;
+
+  if(k->keepon & KEEP_WRITE)
+    fd_write = conn->writesockfd;
+  else
+    fd_write = CURL_SOCKET_BAD;
 
-    /* no read, no exceptions, no timeout */
-    select(conn->writesockfd+1, NULL, writefdp, NULL, &notimeout);
-  }
+  select_res = Curl_select(fd_read, fd_write, 0);
 
   do {
     /* If we still have reading to do, we check if we have a readable
-       socket. Sometimes the reafdp is NULL, if no fd_set was done using
-       the multi interface and then we can do nothing but to attempt a
-       read to be sure. */
-    if((k->keepon & KEEP_READ) &&
-       (!readfdp || FD_ISSET(conn->sockfd, readfdp))) {
+       socket. */
+    if((k->keepon & KEEP_READ) && (select_res & CSELECT_IN)) {
 
       bool is_empty_data = FALSE;
 
@@ -289,7 +268,6 @@
            we bail out from this! */
         else if (0 >= nread) {
           k->keepon &= ~KEEP_READ;
-          FD_ZERO(&k->rkeepfd);
           break;
         }
 
@@ -434,7 +412,6 @@
                 if (k->write_after_100_header) {
 
                   k->write_after_100_header = FALSE;
-                  FD_SET (conn->writesockfd, &k->writefd); /* write */
                   k->keepon |= KEEP_WRITE;
                   k->wkeepfd = k->writefd;
                 }
@@ -451,7 +428,6 @@
                  */
                 k->write_after_100_header = FALSE;
                 k->keepon &= ~KEEP_WRITE;
-                FD_ZERO(&k->wkeepfd);
               }
 
 #ifndef CURL_DISABLE_HTTP
@@ -549,7 +525,6 @@
                 if(stop_reading) {
                   /* we make sure that this socket isn't read more now */
                   k->keepon &= ~KEEP_READ;
-                  FD_ZERO(&k->rkeepfd);
                 }
 
                 break;          /* exit header line loop */
@@ -938,7 +913,6 @@
                   /* Abort after the headers if "follow Location" is set
                      and we're set to close anyway. */
                   k->keepon &= ~KEEP_READ;
-                  FD_ZERO(&k->rkeepfd);
                   *done = TRUE;
                   return CURLE_OK;
                 }
@@ -1027,7 +1001,6 @@
             else if(CHUNKE_STOP == res) {
               /* we're done reading chunks! */
               k->keepon &= ~KEEP_READ; /* read no more */
-              FD_ZERO(&k->rkeepfd);
 
               /* There are now possibly N number of bytes at the end of the
                  str buffer that weren't written to the client, but we don't
@@ -1044,7 +1017,6 @@
               nread = 0;
 
             k->keepon &= ~KEEP_READ; /* we're done reading */
-            FD_ZERO(&k->rkeepfd);
           }
 
           k->bytecount += nread;
@@ -1112,7 +1084,6 @@
           /* if we received nothing, the server closed the connection and we
              are done */
           k->keepon &= ~KEEP_READ;
-          FD_ZERO(&k->rkeepfd);
         }
 
       } while(0);
@@ -1120,11 +1091,8 @@
     } /* if( read from socket ) */
 
     /* If we still have writing to do, we check if we have a writable
-       socket. Sometimes the writefdp is NULL, if no fd_set was done using
-       the multi interface and then we can do nothing but to attempt a
-       write to be sure. */
-    if((k->keepon & KEEP_WRITE) &&
-       (!writefdp || FD_ISSET(conn->writesockfd, writefdp)) ) {
+       socket. */
+    if((k->keepon & KEEP_WRITE) && (select_res & CSELECT_OUT)) {
       /* write */
 
       int i, si;
@@ -1160,7 +1128,6 @@
                  go into the Expect: 100 state and await such a header */
               k->wait100_after_headers = FALSE; /* headers sent */
               k->write_after_100_header = TRUE; /* wait for the header */
-              FD_ZERO (&k->writefd);            /* clear it */
               k->wkeepfd = k->writefd;          /* set the keeper variable */
               k->keepon &= ~KEEP_WRITE;         /* disable writing */
               k->start100 = Curl_tvnow();       /* timeout count starts now */
@@ -1182,7 +1149,6 @@
           if (nread<=0) {
             /* done */
             k->keepon &= ~KEEP_WRITE; /* we're done writing */
-            FD_ZERO(&k->wkeepfd);
             writedone = TRUE;
             break;
           }
@@ -1258,7 +1224,6 @@
           if(k->upload_done) {
             /* switch off writing, we're done! */
             k->keepon &= ~KEEP_WRITE; /* we're done writing */
-            FD_ZERO(&k->wkeepfd);
             writedone = TRUE;
           }
         }
@@ -1300,7 +1265,6 @@
       if(ms > CURL_TIMEOUT_EXPECT_100) {
         /* we've waited long enough, continue anyway */
         k->write_after_100_header = FALSE;
-        FD_SET (conn->writesockfd, &k->writefd); /* write socket */
         k->keepon |= KEEP_WRITE;
         k->wkeepfd = k->writefd;
       }
@@ -1392,13 +1356,10 @@
   /* we want header and/or body, if neither then don't do this! */
   if(conn->bits.getheader || !conn->bits.no_body) {
 
-    FD_ZERO (&k->readfd);               /* clear it */
-    if(conn->sockfd != CURL_SOCKET_BAD) {
-      FD_SET (conn->sockfd, &k->readfd); /* read socket */
+    if(conn->sockfd != CURL_SOCKET_BAD) { 
       k->keepon |= KEEP_READ;
     }
 
-    FD_ZERO (&k->writefd);              /* clear it */
     if(conn->writesockfd != CURL_SOCKET_BAD) {
       /* HTTP 1.1 magic:
 
@@ -1420,7 +1381,6 @@
           /* when we've sent off the rest of the headers, we must await a
              100-continue */
           k->wait100_after_headers = TRUE;
-        FD_SET (conn->writesockfd, &k->writefd); /* write socket */
         k->keepon |= KEEP_WRITE;
       }
     }
@@ -1508,13 +1468,23 @@
   k->readfdp = &k->readfd;   /* store the address of the set */
 
   while (!done) {
-    struct timeval interval;
-    k->readfd = k->rkeepfd;  /* set these every lap in the loop */
-    k->writefd = k->wkeepfd;
-    interval.tv_sec = 1;
-    interval.tv_usec = 0;
+    int fd_read;
+    int fd_write;
+    int interval_ms;
+
+    interval_ms = 1 * 1000;
+
+    if(k->keepon & KEEP_READ)
+      fd_read = conn->sockfd;
+    else
+      fd_read = CURL_SOCKET_BAD;
+
+    if(k->keepon & KEEP_WRITE)
+      fd_write = conn->writesockfd;
+    else
+      fd_write = CURL_SOCKET_BAD;
 
-    switch (select (k->maxfd, k->readfdp, k->writefdp, NULL, &interval)) {
+    switch (Curl_select(fd_read, fd_write, interval_ms)) {
     case -1: /* select() error, stop reading */
 #ifdef EINTR
       /* The EINTR is not serious, and it seems you might get this more
diff -urN curl-7.12.2-orig/lib/url.c curl-7.12.2/lib/url.c
--- curl-7.12.2-orig/lib/url.c	2004-10-17 17:21:02.000000000 -0500
+++ curl-7.12.2/lib/url.c	2004-11-16 19:11:21.000000000 -0600
@@ -61,10 +61,6 @@
 #include <sys/param.h>
 #endif
 
-#ifdef HAVE_SYS_SELECT_H
-#include <sys/select.h>
-#endif
-
 #ifdef VMS
 #include <in.h>
 #include <inet.h>
@@ -74,9 +70,6 @@
 #include <setjmp.h>
 #endif
 
-#ifndef HAVE_SELECT
-#error "We can't compile without select() support!"
-#endif
 #ifndef HAVE_SOCKET
 #error "We can't compile without socket() support!"
 #endif
@@ -124,6 +117,7 @@
 #include "content_encoding.h"
 #include "http_digest.h"
 #include "http_negotiate.h"
+#include "select.h"
 
 /* And now for the protocols */
 #include "ftp.h"
@@ -1547,16 +1541,8 @@
 {
   int sval;
   bool ret_val = TRUE;
-  fd_set check_set;
-  struct timeval to;
-
-  FD_ZERO(&check_set);
-  FD_SET(sock, &check_set);
-
-  to.tv_sec = 0;
-  to.tv_usec = 0;
 
-  sval = select(sock + 1, &check_set, 0, 0, &to);
+  sval = Curl_select(sock, CURL_SOCKET_BAD, 0);
   if(sval == 0)
     /* timeout */
     ret_val = FALSE;
diff -urN curl-7.12.2-orig/tests/data/test515 curl-7.12.2/tests/data/test515
--- curl-7.12.2-orig/tests/data/test515	1969-12-31 18:00:00.000000000 -0600
+++ curl-7.12.2/tests/data/test515	2004-11-17 14:04:30.000000000 -0600
@@ -0,0 +1,48 @@
+#
+# Server-side
+<reply name="1">
+<data>
+HTTP/1.1 200 OK
+Date: Thu, 09 Nov 2010 14:49:00 GMT
+Server: test-server/fake
+Last-Modified: Tue, 13 Jun 2000 12:10:00 GMT
+ETag: "21025-dc7-39462498"
+Accept-Ranges: bytes
+Content-Length: 6
+Connection: close
+Content-Type: text/html
+Funny-head: yesyes
+
+<foo>
+</data>
+</reply>
+
+# Client-side
+<client>
+<server>
+http
+</server>
+# tool is what to use instead of 'curl'
+<tool>
+lib500
+</tool>
+
+ <name>
+simple libcurl HTTP GET tool with more than FD_SETSIZE descriptors
+ </name>
+ <command>
+http://%HOSTIP:%HTTPPORT/500
+</command>
+</client>
+
+#
+# Verify data after the test has been "shot"
+<verify>
+<protocol>
+GET /500 HTTP/1.1
+Host: 127.0.0.1:%HTTPPORT
+Pragma: no-cache
+Accept: */*
+
+</protocol>
+</verify>
diff -urN curl-7.12.2-orig/tests/libtest/lib515.c curl-7.12.2/tests/libtest/lib515.c
--- curl-7.12.2-orig/tests/libtest/lib515.c	1969-12-31 18:00:00.000000000 -0600
+++ curl-7.12.2/tests/libtest/lib515.c	2004-11-17 13:00:57.000000000 -0600
@@ -0,0 +1,47 @@
+#include "test.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include <mprintf.h>
+
+#ifdef HAVE_SYS_SELECT_H
+#include <sys/select.h>
+#endif
+
+#ifndef FD_SETSIZE
+#error "this test requires FD_SETSIZE"
+#endif
+
+#define NUM_OPEN (FD_SETSIZE + 10)
+
+int test(char *URL)
+{
+  CURLcode res;
+  CURL *curl;
+  int fd[NUM_OPEN];
+  int i;
+
+  /* open a lot of file descriptors */
+  for (i = 0; i < NUM_OPEN; i++) {
+    fd[i] = open("/dev/null", O_RDONLY);
+    if (fd[i] == -1) {
+      fprintf(stderr, "open: attempt #%i: failed to open /dev/null\n", i);
+      for (i--; i >= 0; i--)
+        close(fd[i]);
+      return CURLE_FAILED_INIT;
+    }
+  }
+
+  curl = curl_easy_init();
+  curl_easy_setopt(curl, CURLOPT_URL, URL);
+  curl_easy_setopt(curl, CURLOPT_HEADER, TRUE);
+  res = curl_easy_perform(curl);
+  curl_easy_cleanup(curl);  
+
+  for (i = 0; i < NUM_OPEN; i++)
+    close(fd[i]);
+
+  return (int)res;
+}
diff -urN curl-7.12.2-orig/tests/libtest/Makefile.am curl-7.12.2/tests/libtest/Makefile.am
--- curl-7.12.2-orig/tests/libtest/Makefile.am	2004-06-30 06:09:16.000000000 -0500
+++ curl-7.12.2/tests/libtest/Makefile.am	2004-11-17 12:47:52.000000000 -0600
@@ -39,7 +39,7 @@
 
 # These are all libcurl test programs
 noinst_PROGRAMS = lib500 lib501 lib502 lib503 lib504 lib505 lib506 lib507 \
-  lib508 lib509 lib510 lib511 lib512 lib513 lib514
+  lib508 lib509 lib510 lib511 lib512 lib513 lib514 lib515
 
 lib500_SOURCES = lib500.c $(SUPPORTFILES)
 lib500_LDADD = $(LIBDIR)/libcurl.la
@@ -100,3 +100,7 @@
 lib514_SOURCES = lib514.c $(SUPPORTFILES)
 lib514_LDADD = $(LIBDIR)/libcurl.la
 lib514_DEPENDENCIES = $(LIBDIR)/libcurl.la
+
+lib515_SOURCES = lib515.c $(SUPPORTFILES)
+lib515_LDADD = $(LIBDIR)/libcurl.la
+lib515_DEPENDENCIES = $(LIBDIR)/libcurl.la

