updates.
[silc.git] / lib / silcutil / win32 / silcwin32sockconn.c
index 8ef70c8b0462a8a34f63f0b8c2379d7fdb700480..7f604c0c6c8b19cf73bfc08668ff8f77a2e4e3a7 100644 (file)
@@ -33,10 +33,13 @@ int silc_socket_write(SilcSocketConnection sock)
   SOCKET fd = sock->sock;
   SilcBuffer src = sock->outbuf;
 
+  if (SILC_IS_DISABLED(sock))
+    return -1;
+
   SILC_LOG_DEBUG(("Writing data to socket %d", fd));
 
   if (src->len > 0) {
-    ret = recv(fd, src->data, src->len,  0);
+    ret = send(fd, src->data, src->len,  0);
     if (ret == SOCKET_ERROR) {
       err = WSAGetLastError();
       if (err == WSAEWOULDBLOCK) {
@@ -65,9 +68,23 @@ int silc_socket_read(SilcSocketConnection sock)
   int len = 0, err;
   unsigned char buf[SILC_SOCKET_READ_SIZE];
   SOCKET fd = sock->sock;
+  int argp;
+
+  if (SILC_IS_DISABLED(sock))
+    return -1;
 
   SILC_LOG_DEBUG(("Reading data from socket %d", fd));
 
+  /* Check whether there is data available, without calling recv(). */
+  ioctlsocket(fd, FIONREAD, (unsigned long *)&argp);
+  if (argp == 0) {
+    /* Is this kludge or what? Without this thing this contraption
+       does not work at all!?. */
+    SleepEx(1, TRUE);
+    SILC_LOG_DEBUG(("Could not read immediately, will do it later"));
+    return -2;
+  }
+
   /* Read the data from the socket. */
   len = recv(fd, buf, sizeof(buf), 0);
   if (len == SOCKET_ERROR) {