--- session.c.original  2010-03-22 18:21:33.000000000 -0700
+++ session.c   2010-03-22 18:20:53.000000000 -0700
@@ -518,11 +518,28 @@
  */
 int _libssh2_wait_socket(LIBSSH2_SESSION *session)
 {
+    int rc;
+    int dir;
+#if HAVE_POLL
+    struct pollfd sockets[1];
+    sockets[0].fd = session->socket_fd;
+    sockets[0].events = 0;
+    sockets[0].revents = 0;
+
+    /* now make sure we wait in the correct direction */
+    dir = libssh2_session_block_directions(session);
+    
+    if(dir & LIBSSH2_SESSION_BLOCK_INBOUND)
+      sockets[0].events |= POLLIN;
+    
+    if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
+      sockets[0].events |= POLLOUT;
+  
+    rc = poll(sockets, 1, -1);
+#else
     fd_set fd;
     fd_set *writefd = NULL;
     fd_set *readfd = NULL;
-    int dir;
-    int rc;
 
     FD_ZERO(&fd);
     FD_SET(session->socket_fd, &fd);
@@ -539,6 +556,7 @@
     /* Note that this COULD be made to use a timeout that perhaps could be
        customizable by the app or something... */
     rc = select(session->socket_fd + 1, readfd, writefd, NULL, NULL);
+#endif
 
     if(rc <= 0) {
         /* timeout (or error), bail out with a timeout error */

