title: Winsock accept() returning WSAENOTSOCK (code 10038)
body: <p>hope you're having a good day. Another socket issue, another day :)</p>
  
  <p>I finally got MicroSoft Visual C++ (MSVC++) IDE installed, plus the Platform SDK, so I can compile winsock applications.</p>
  
  <p>Missed a chunk of stuff here. In the ServerSocket::accept() function, it creates a new instance of ClientSocket and sets it's socket file descriptor to the one that was accept()ed, I have also checked there and it's recognizing that the descriptor is valid there as well.</p>
  
  <p>In my ClientSocket::recv() function, I call (obviously) the recv() function out of the winsock library. The issue I am having is that the socket descriptor I am using is being recognized by recv() as invalid, but only on the server-side ClientSocket instance returned from my ServerSocket::accept() - The client-side ClientSocket instance has no problems. I inserted multiple debug statements, the descriptor is valid. </p>
  
  <p>The weirdest bit about this is that if I compile this <em>exact same code</em> with MinGW gcc/g++ on windows, it runs fine! It's only using MSVC++ that this problem occurs.</p>
  
  <pre><code>string ClientSocket::recv(int bufsize) {
      if (!isConnected()) throw SocketException("Not connected.");
  
      cout &lt;&lt; "SocketRecv: " &lt;&lt; (sockfd == INVALID_SOCKET) &lt;&lt; " " &lt;&lt; sockfd &lt;&lt; endl;
      vector&lt;char&gt; buffer(bufsize+1, 0);
      cout &lt;&lt; "SocketRecv1: " &lt;&lt; (sockfd == INVALID_SOCKET) &lt;&lt; " " &lt;&lt; sockfd &lt;&lt; endl;
      int ret = ::recv(sockfd, &amp;buffer[0], bufsize, 0);
      cout &lt;&lt; "SocketRecv2: " &lt;&lt; (sockfd == INVALID_SOCKET) &lt;&lt; " " &lt;&lt; sockfd &lt;&lt; endl;
  
      // ret is apparently -1 because of "invalid" socket descriptor, but the
      // above statements print zero (false) on the (sockfd == INVALID_SOCKET) ! :\
      if (ret &lt; 0) {
          #ifdef _WIN32
          switch((ret = WSAGetLastError())) {
          #else
          switch(errno) {
          #endif
              case DECONNREFUSED: // The 'd' prefix means _I_ defined it, i.e. from windows it's
                                  // set to 'WSAECONNREFUSED', but from linux it's set to 'ECONNREFUSED'
                  throw SocketException("Connection refused on recover.");
                  break;
              case DENOTCONN:
                  throw SocketException("Not connected.");
                  break;
              case DECONNABORTED:
                  throw SocketException("Software caused connection abort.");
                  break;
              case DECONNRESET:
                  throw SocketException("Connection reset by peer.");
                  break;
              default:
                  //usually this itoa() and char/string stuff isn't here... needed it in 
                  //order to find out what the heck the problem was.
                  char tmp[21];
                  string tmp4 = "Unknown error reading socket. ";
                  string tmp3 = tmp4 + itoa(ret, tmp, 10);
                  //this throw keeps throwing "Unknown error reading socket. 10038"
                  throw SocketException(tmp3); 
                  break;
          }
      } else if (ret == 0) {
          connected = false;
          return "";
      }
  
      return &amp;buffer[0];
  }
  </code></pre>
  
  <p>Additional information: The socket is in blocking mode, i.e. has not been set to non-blocking. I have called WSAStartup() successfully. This is happening on the server side, on the ClientSocket instance returned from my ServerSocket::accept() (yes, I checked the descriptor there too - it's fine). The client side claims 'WSAECONNRESET (10054)' or 'WSAECONNABORTED (10053)'.</p>
  
  <p>I can't think of anything else that could be wrong. The worst part is, it works fine using MinGW gcc/g++ on windows and linux both. </p>
  
  <p>If you want to see the whole library, it's pasted at: (caution: 600+ lines!) <br>
  Socket.cxx - <a href="http://paste.pocoo.org/show/353725/" rel="nofollow">http://paste.pocoo.org/show/353725/</a> <br>
  Socket.hxx - <a href="http://paste.pocoo.org/show/353726/" rel="nofollow">http://paste.pocoo.org/show/353726/</a></p>
  
  <p>Thanks!!!</p>
  
  <p><strong>Update</strong> - As per Ben's solution, I am now using: <code>void ServerSocket::accept(ClientSocket&amp; sock);</code>, and implementing as: <code>ClientSocket mysock; server.accept(mysock);</code> </p>
  
  <p>Thank you so much!!!</p>