diff -ur libssh2-1.0.1-20090318/src/libssh2_priv.h libssh2/src/libssh2_priv.h
--- libssh2-1.0.1-20090318/src/libssh2_priv.h	Tue Mar 17 22:02:04 2009
+++ libssh2/src/libssh2_priv.h	Wed Mar 18 09:15:48 2009
@@ -141,12 +141,14 @@
 #define inline __inline
 #endif
 
+#ifndef HAVE_UNISTD_H
 /* not really usleep, but safe for the way we use it in this lib */
 static inline int usleep(int udelay)
 {
 	Sleep(udelay / 1000);
 	return 0;
 }
+#endif
 
 #endif
 
@@ -1141,6 +1143,8 @@
 libssh2_uint64_t _libssh2_ntohu64(const unsigned char *buf);
 void _libssh2_htonu32(unsigned char *buf, unsigned long val);
 void _libssh2_htonu64(unsigned char *buf, libssh2_uint64_t val);
+ssize_t _libssh2_recv(int socket, void *buffer, size_t length, int flags);
+ssize_t _libssh2_send(int socket, const void *buffer, size_t length, int flags);
 
 #define LIBSSH2_READ_TIMEOUT 60 /* generic timeout in seconds used when
                                    waiting for more data to arrive */
diff -ur libssh2-1.0.1-20090318/src/misc.c libssh2/src/misc.c
--- libssh2-1.0.1-20090318/src/misc.c	Tue Mar 17 22:02:04 2009
+++ libssh2/src/misc.c	Wed Mar 18 09:12:54 2009
@@ -45,6 +45,90 @@
 #include <sys/time.h>
 #endif
 
+#include <errno.h>
+
+
+/* _libssh2_recv
+ *
+ * Wrapper around standard recv to allow WIN32 systems 
+ * to set errno
+ */
+ssize_t
+_libssh2_recv(int socket, void *buffer, size_t length, int flags)
+{
+    ssize_t rc = recv(socket, buffer, length, flags);
+#ifdef WIN32
+    if (rc < 0 ) {
+        switch (WSAGetLastError()) {
+        case WSAEWOULDBLOCK:
+            errno = EAGAIN;
+            break;
+
+        case WSAENOTSOCK:
+            errno = EBADF;
+            break;
+
+        case WSAENOTCONN:
+        case WSAECONNABORTED:
+            errno = WSAENOTCONN;
+            break;
+
+        case WSAEINTR:
+            errno = EINTR;
+            break;
+
+        default:
+            /* it is most important to ensure errno does not stay at EAGAIN when a different error occurs */
+            /* so just set errno to a generic error */
+            errno = EIO;
+        }
+    }
+#endif
+    return rc;
+}
+
+
+
+/* _libssh2_send
+ *
+ * Wrapper around standard send to allow WIN32 systems 
+ * to set errno
+ */
+ssize_t
+_libssh2_send(int socket, const void *buffer, size_t length, int flags)
+{
+    ssize_t rc = send(socket, buffer, length, flags);
+#ifdef WIN32
+    if (rc < 0 ) {
+        switch (WSAGetLastError()) {
+        case WSAEWOULDBLOCK:
+            errno = EAGAIN;
+            break;
+
+        case WSAENOTSOCK:
+            errno = EBADF;
+            break;
+
+        case WSAENOTCONN:
+        case WSAECONNABORTED:
+            errno = WSAENOTCONN;
+            break;
+
+        case WSAEINTR:
+            errno = EINTR;
+            break;
+
+        default:
+            /* it is most important to ensure errno does not stay at EAGAIN when a different error occurs */
+            /* so just set errno to a generic error */
+            errno = EIO;
+        }
+    }
+#endif
+    return rc;
+}
+
+
 /* libssh2_ntohu32
  */
 unsigned long
diff -ur libssh2-1.0.1-20090318/src/session.c libssh2/src/session.c
--- libssh2-1.0.1-20090318/src/session.c	Tue Mar 17 22:02:04 2009
+++ libssh2/src/session.c	Wed Mar 18 09:36:25 2009
@@ -104,30 +104,10 @@
         char c = '\0';
 
         ret =
-            recv(session->socket_fd, &c, 1,
+            _libssh2_recv(session->socket_fd, &c, 1,
                  LIBSSH2_SOCKET_RECV_FLAGS(session));
 
         if (ret < 0) {
-#ifdef WIN32
-            switch (WSAGetLastError()) {
-            case WSAEWOULDBLOCK:
-                errno = EAGAIN;
-                break;
-
-            case WSAENOTSOCK:
-                errno = EBADF;
-                break;
-
-            case WSAENOTCONN:
-            case WSAECONNABORTED:
-                errno = WSAENOTCONN;
-                break;
-
-            case WSAEINTR:
-                errno = EINTR;
-                break;
-            }
-#endif /* WIN32 */
             if (errno == EAGAIN) {
                 session->socket_block_directions =
                     LIBSSH2_SESSION_BLOCK_INBOUND;
@@ -226,7 +206,7 @@
     }
 
     ret =
-        send(session->socket_fd, banner + session->banner_TxRx_total_send,
+        _libssh2_send(session->socket_fd, banner + session->banner_TxRx_total_send,
              banner_len - session->banner_TxRx_total_send,
              LIBSSH2_SOCKET_SEND_FLAGS(session));
 
@@ -1505,7 +1485,7 @@
                 case LIBSSH2_POLLFD_CHANNEL:
                     if (FD_ISSET(fds[i].fd.channel->session->socket_fd, &rfds)) {
                         /* Spin session until no data available */
-                        while (libssh2_packet_read(fds[i].fd.channel->session)
+                        while (_libssh2_packet_read(fds[i].fd.channel->session)
                                > 0);
                     }
                     break;
@@ -1514,7 +1494,7 @@
                     if (FD_ISSET
                         (fds[i].fd.listener->session->socket_fd, &rfds)) {
                         /* Spin session until no data available */
-                        while (libssh2_packet_read(fds[i].fd.listener->session)
+                        while (_libssh2_packet_read(fds[i].fd.listener->session)
                                > 0);
                     }
                     break;
diff -ur libssh2-1.0.1-20090318/src/transport.c libssh2/src/transport.c
--- libssh2-1.0.1-20090318/src/transport.c	Tue Mar 17 22:02:04 2009
+++ libssh2/src/transport.c	Wed Mar 18 09:10:04 2009
@@ -360,32 +360,12 @@
 
             /* now read a big chunk from the network into the temp buffer */
             nread =
-                recv(session->socket_fd, &p->buf[remainbuf],
+                _libssh2_recv(session->socket_fd, &p->buf[remainbuf],
                      PACKETBUFSIZE - remainbuf,
                      LIBSSH2_SOCKET_RECV_FLAGS(session));
             if (nread <= 0) {
                 /* check if this is due to EAGAIN and return the special
                    return code if so, error out normally otherwise */
-#ifdef WIN32
-                switch (WSAGetLastError()) {
-                case WSAEWOULDBLOCK:
-                    errno = EAGAIN;
-                    break;
-
-                case WSAENOTSOCK:
-                    errno = EBADF;
-                    break;
-
-                case WSAENOTCONN:
-                case WSAECONNABORTED:
-                    errno = WSAENOTCONN;
-                    break;
-
-                case WSAEINTR:
-                    errno = EINTR;
-                    break;
-                }
-#endif /* WIN32 */
                 if ((nread < 0) && (errno == EAGAIN)) {
                     session->socket_block_directions =
                         LIBSSH2_SESSION_BLOCK_INBOUND;
@@ -620,7 +600,7 @@
     /* number of bytes left to send */
     length = p->ototal_num - p->osent;
 
-    rc = send(session->socket_fd, &p->outbuf[p->osent], length,
+    rc = _libssh2_send(session->socket_fd, &p->outbuf[p->osent], length,
               LIBSSH2_SOCKET_SEND_FLAGS(session));
 
     if (rc == length) {
@@ -785,7 +765,7 @@
 
     session->local.seqno++;
 
-    ret = send(session->socket_fd, p->outbuf, total_length,
+    ret = _libssh2_send(session->socket_fd, p->outbuf, total_length,
                LIBSSH2_SOCKET_SEND_FLAGS(session));
 
     if (ret != -1) {
