title: WinSock2 Error 10093 on recv
body: <p>So I'm building a multithreaded socket library in windows and I'm getting the WSA Not Started error when I call recv, even though I successfully get a client to connect to the server. I also had it working before I threaded it, but I don't know what happened since then. Any help would be appreciated.</p>
  
  <p>Spocket.hpp</p>
  
  <pre><code>    #include &lt;iostream&gt;
      #include &lt;string&gt;
      #include &lt;Windows.h&gt;
      #pragma comment (lib,"ws2_32.lib")
  
      static bool initialized_ = false;
  
      class Spocket
      {
      protected:
          WSADATA         wsaData_;
          SOCKET          hSocket_;
          sockaddr_in     service_;
          std::string     addr_;
          USHORT          port_;
          int             exitCode_;
  
      public:
          Spocket() { 
              initialize(); 
              create_socket(); 
          }
          Spocket(std::string addr, USHORT port)
              : addr_( addr ), port_( port ) { 
                  initialize(); 
                  create_socket(); 
          }
          Spocket(Spocket spock, SOCKET sock)
              : hSocket_( sock ), 
              wsaData_( spock.wsaData_ ),
              service_( spock.service_ ),
              addr_( spock.addr_ ),
              port_( spock.port_ ) 
          { 
              initialize(); 
          }
          virtual ~Spocket() { close(); }
  
          void initialize();
          void create_socket();
          void close();
  
          template&lt;typename T&gt;
          int recv_data(T* i) {
              int ret = recv( hSocket_, reinterpret_cast&lt;char*&gt;(i), 32, 0 );
              if( ret == SOCKET_ERROR )
                  cerr &lt;&lt; WSAGetLastError() &lt;&lt; endl;
              return ret;
          }
  
          template&lt;typename T&gt;
          int send_data(T* i) {
              int ret = send( hSocket_, reinterpret_cast&lt;char*&gt;(i), sizeof(i), 0 );
              if( ret == SOCKET_ERROR )
                  cerr &lt;&lt; WSAGetLastError() &lt;&lt; endl;
              return ret;
          }
      };
  
  class ServerSpocket : public Spocket
  {
  public:
      ServerSpocket(std::string addr, USHORT port);
      Spocket* accept_clients();
  };
  
  
  class ClientSpocket : public Spocket
  {
  public:
      ClientSpocket(std::string addr, USHORT port);
  };
  </code></pre>
  
  <p>Spocket.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  #include "../include/spocket.hpp"
  
  void Spocket::initialize() {
      if(!initialized_)
      {
          cout &lt;&lt; "Initializing socket..." &lt;&lt; endl;
  
          exitCode_ = EXIT_SUCCESS;
          int iResult = WSAStartup( MAKEWORD(2,2), &amp;wsaData_ );
          if( iResult != NO_ERROR ) {
              cerr &lt;&lt; "WSAStartup failed" &lt;&lt; endl;
              exitCode_ = EXIT_FAILURE;
              close();
          }
          initialized_ = true;
      }
  }
  
  void Spocket::create_socket() {
      hSocket_ = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
      if( hSocket_ == INVALID_SOCKET )
      {
          cerr &lt;&lt; "Error at socket(): " &lt;&lt; WSAGetLastError() &lt;&lt; endl;
          exitCode_ = EXIT_FAILURE;
          close();
      }
  
      service_.sin_family = AF_INET;
      service_.sin_addr.s_addr = inet_addr(addr_.c_str());
      service_.sin_port = htons(port_);
  }
  
  void Spocket::close() {
      closesocket( hSocket_ );
      WSACleanup();
  }
  
  ServerSpocket::ServerSpocket(std::string addr, USHORT port) : Spocket(addr, port) {
      if( bind( hSocket_, (SOCKADDR*)&amp;service_, sizeof(service_) ) == SOCKET_ERROR )
      {
          cerr &lt;&lt; "Failed to bind" &lt;&lt; endl;
          exitCode_ = EXIT_FAILURE;
          close();
      }
  
      if( listen( hSocket_, 1 ) == SOCKET_ERROR )
      {
          cerr &lt;&lt; "Error listening on socket" &lt;&lt; endl;
          exitCode_ = EXIT_FAILURE;
          close();
      }
  }
  
  Spocket* ServerSpocket::accept_clients() {
      cout &lt;&lt; "Waiting for connection...\n";
      SOCKET hAccepted = INVALID_SOCKET;
      while( hAccepted == INVALID_SOCKET )
          hAccepted = accept( hSocket_, NULL, NULL );
      return new Spocket( *this, hAccepted );
  }
  
  ClientSpocket::ClientSpocket(std::string addr, USHORT port) : Spocket(addr, port) {
      if( connect( hSocket_, (SOCKADDR*)&amp;service_, sizeof(service_) ) == SOCKET_ERROR )
      {
          cerr &lt;&lt; "Failed to connect" &lt;&lt; endl;
          exitCode_ = EXIT_FAILURE;
          close();
      }
  }
  </code></pre>
  
  <p>Server_main.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;vector&gt;
  #include &lt;spocket.hpp&gt;
  using namespace std;
  
  vector&lt;HANDLE&gt; hThreads;
  vector&lt;DWORD&gt; dwThreadIds;
  
  struct ConnectionInfo
  {
      string ip;
      unsigned int port;
      ConnectionInfo( string ip_, unsigned int port_ ) : ip(ip_), port(port_){}
  };
  static ConnectionInfo ci( "127.0.0.1", 27015 );
  
  DWORD WINAPI clientSession( LPVOID lpParam )
  {
      // create new socket to listen for connection attempts
      ConnectionInfo arg = *reinterpret_cast&lt;ConnectionInfo*&gt;(lpParam);
      ServerSpocket listenSock( arg.ip, arg.port );
  
      // spawn a duplicate thread when a connection is made, and close the current listening socket
      Spocket* sessionSock = listenSock.accept_clients();
      listenSock.close();
      cout &lt;&lt; "client connected..." &lt;&lt; endl;
      /*
      hThreads.push_back( CreateThread( NULL, 0, clientSession, &amp;ci, 0, NULL ) );
      */
  
      // service the connected client
      string msg;
      while( sessionSock-&gt;recv_data(&amp;msg) != SOCKET_ERROR &amp;&amp; msg != "goodbye!" )
      {
          cout &lt;&lt; msg &lt;&lt; endl;
          msg.clear();
      }
  
      cout &lt;&lt; "finished with client..." &lt;&lt; endl;
  
      // wait quietly for server shutdown
      while( true )
          Sleep( 200 );
      return 0;
  }
  
  int main() {
      cout &lt;&lt; "[Server]" &lt;&lt; endl;
      cout &lt;&lt; "starting up..." &lt;&lt; endl;
  
      hThreads.push_back( CreateThread( NULL, 0, clientSession, &amp;ci, 0, NULL ) );
  
      string input = "";
      do 
          cin &gt;&gt; input;
      while( input != "exit" );
  
      // close all thread handles here
      cout &lt;&lt; "shutting down..." &lt;&lt; endl;
      return 0;
  }
  </code></pre>