--- nanoftp.c.orig	2008-08-29 21:37:00.000000000 +0200
+++ nanoftp.c	2009-08-23 13:49:50.000000000 +0200
@@ -49,9 +49,13 @@
 #ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
 #endif
+#ifndef HAVE_POLL_H
 #ifdef HAVE_SYS_SELECT_H
 #include <sys/select.h>
 #endif
+#else
+#include <poll.h>
+#endif
 #ifdef HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
 #endif
@@ -734,15 +738,26 @@
 int
 xmlNanoFTPCheckResponse(void *ctx) {
     xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
+    #ifndef HAVE_POLL_H
     fd_set rfd;
     struct timeval tv;
+    #else
+    struct pollfd p;
+    #endif
 
     if ((ctxt == NULL) || (ctxt->controlFd < 0)) return(-1);
+    #ifndef HAVE_POLL_H
+    if (ctxt->controlFd > FD_SETSIZE) return -1;
+
     tv.tv_sec = 0;
     tv.tv_usec = 0;
     FD_ZERO(&rfd);
     FD_SET(ctxt->controlFd, &rfd);
     switch(select(ctxt->controlFd + 1, &rfd, NULL, NULL, &tv)) {
+    #else
+    p.fd = ctxt->controlFd; p.events = POLLIN;
+    switch(poll(& p, 1, 0)) {
+    #endif
 	case 0:
 	    return(0);
 	case -1:
@@ -1539,12 +1554,22 @@
 xmlNanoFTPCloseConnection(void *ctx) {
     xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
     int res;
+    #ifndef HAVE_POLL_H
     fd_set rfd, efd;
     struct timeval tv;
+    #else
+    struct pollfd p;
+    #endif
 
     if ((ctxt == NULL) || (ctxt->controlFd < 0)) return(-1);
 
     closesocket(ctxt->dataFd); ctxt->dataFd = -1;
+    #ifndef HAVE_POLL_H
+    if (ctxt->controlFd > FD_SETSIZE) {
+        /* avoid descriptor leak */
+        closesocket(ctxt->controlFd); ctxt->controlFd = -1;
+        return -1;
+    }
     tv.tv_sec = 15;
     tv.tv_usec = 0;
     FD_ZERO(&rfd);
@@ -1552,6 +1577,10 @@
     FD_ZERO(&efd);
     FD_SET(ctxt->controlFd, &efd);
     res = select(ctxt->controlFd + 1, &rfd, NULL, &efd, &tv);
+    #else
+    p.fd = ctxt->controlFd; p.events = POLLIN;
+    res = poll(& p, 1, 15000);
+    #endif
     if (res < 0) {
 #ifdef DEBUG_FTP
 	perror("select");
@@ -1721,8 +1750,12 @@
     char buf[4096 + 1];
     int len, res;
     int indx = 0, base;
+    #ifndef HAVE_POLL_H
     fd_set rfd, efd;
     struct timeval tv;
+    #else
+    struct pollfd p;
+    #endif
 
     if (ctxt == NULL) return (-1);
     if (filename == NULL) {
@@ -1760,6 +1793,12 @@
     }
 
     do {
+    #ifndef HAVE_POLL_H
+    if (ctxt->dataFd > FD_SETSIZE) {
+        /* avoid descriptor leak */
+        closesocket(ctxt->dataFd); ctxt->dataFd = -1;
+        return -1;
+    }
 	tv.tv_sec = 1;
 	tv.tv_usec = 0;
 	FD_ZERO(&rfd);
@@ -1767,6 +1806,10 @@
 	FD_ZERO(&efd);
 	FD_SET(ctxt->dataFd, &efd);
 	res = select(ctxt->dataFd + 1, &rfd, NULL, &efd, &tv);
+    #else
+    p.fd = ctxt->dataFd; p.events = POLLIN;
+    res = poll(& p, 1, 60000);
+    #endif
 	if (res < 0) {
 #ifdef DEBUG_FTP
 	    perror("select");
@@ -1895,8 +1938,12 @@
     xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
     char buf[4096];
     int len = 0, res;
+    #ifndef HAVE_POLL_H
     fd_set rfd;
     struct timeval tv;
+    #else
+    struct pollfd p;
+    #endif
 
     if (ctxt == NULL) return(-1);
     if ((filename == NULL) && (ctxt->path == NULL))
@@ -1907,11 +1954,21 @@
 	return(-1);
 
     do {
+    #ifndef HAVE_POLL_H
+    if (ctxt->dataFd > FD_SETSIZE) {
+        /* avoid descriptor leak */
+        closesocket(ctxt->dataFd); ctxt->dataFd = -1;
+        return -1;
+    }
 	tv.tv_sec = 1;
 	tv.tv_usec = 0;
 	FD_ZERO(&rfd);
 	FD_SET(ctxt->dataFd, &rfd);
 	res = select(ctxt->dataFd + 1, &rfd, NULL, NULL, &tv);
+    #else
+    p.fd = ctxt->dataFd; p.events = POLLIN;
+    res = poll(& p, 1, 60000);
+    #endif
 	if (res < 0) {
 #ifdef DEBUG_FTP
 	    perror("select");
