title: Why do I get the WSAENOTSOCK error in this code?
body: <p>Here is Main.cpp up to the point where the error happens:</p>
  
  <pre><code>int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd)
  {
      if(SUCCEEDED(CoInitialize(NULL)))
      {
          {
              Game game;
  
              game.CreateRessources(hInst);
  
              game.ShowMainScreen();
  
              game.pWinsock-&gt;Initialize(game.Getm_hWnd());
  </code></pre>
  
  <p>game.Getm_hWnd return the private HWND m_hWnd.</p>
  
  <p>Here is Game::CreateRessources(HINSTANCE):</p>
  
  <pre><code>void Game::CreateRessources(HINSTANCE hInst)
  {   
      m_hWnd=CreateWindowClass(hInst);
  
      pMessageLog=CreateMessageLog();
      pD2DResources=CreateD2DResources(m_hWnd);
      pWinsock=CreateWinsock();
  
  }
  </code></pre>
  
  <p>There is CreateWinsock():</p>
  
  <pre><code>Winsock* CreateWinsock()
  {
      Winsock* pWinsock=new Winsock;
  
      return pWinsock;
  }
  </code></pre>
  
  <p>Winsock::Winsock:</p>
  
  <blockquote>
    <p>Winsock::Winsock() :  Socket(NULL) { }</p>
  </blockquote>
  
  <p>And finally, Winsock::Initialize(HWND):</p>
  
  <pre><code>void Winsock::Initialize(HWND hwnd)
  {
      WSADATA wsaDat;
  
      SendMessage(hwnd, LOG_ADD, NULL, (LPARAM)L"Initializing winsock... ");
  
      int nResult = WSAStartup(MAKEWORD(2,2),&amp;wsaDat);
      if(nResult!=0)
      {
          MessageBox(NULL,"Winsock initialization failed","Critical error",MB_ICONERROR);
          SendMessage(hwnd,WM_DESTROY,NULL,NULL);
      }
  
      SendMessage(hwnd, LOG_ADD, NULL, (LPARAM)L"Done!\nCreating a socket... ");
  
      Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
      if(Socket==INVALID_SOCKET)
      {
          MessageBox(NULL,"Socket Creation failed","Critical error",MB_ICONERROR);
          SendMessage(hwnd,WM_DESTROY,NULL,NULL);
      }
  
      SendMessage(hwnd, LOG_ADD, NULL, (LPARAM)L"Done!\nRequesting Windows message-based notification of network events... ");
  
      nResult=WSAAsyncSelect(Socket,hwnd,WM_SOCKET,(FD_CLOSE|FD_READ));
      if(nResult)
      {
          if(WSAGetLastError()==WSAENOTSOCK)
              MessageBox(hwnd,"WSAENOTSOCK Error!","Error",NULL);
          MessageBox(NULL,"WSAAsyncSelect failed","Critical error",MB_ICONERROR);
          SendMessage(hwnd,WM_DESTROY,NULL,NULL);
      }
  
      /* More code */
  }
  </code></pre>
  
  <p>The line if(WSAGetLastError()==WSAENOTSOCK) returns true. WSAENOTSOCK means the following:</p>
  
  <p>"Socket operation on nonsocket.
  An operation was attempted on something that is not a socket. Either the socket handle parameter did not reference a valid socket, or for select, a member of an fd_set was not valid."</p>
  
  <p>Edit: There is my Winsock class:</p>
  
  <pre><code>class Winsock{
  public:
      Winsock();
  
      void Initialize(HWND);
  
      void ReceiveMsg();
  
  private:
      SOCKET Socket;
      static const char* server;
      static const int port;
  };
  </code></pre>
  
  <p>As far as I can tell, Socket IS a socket, and a valid one. How comes I receive that error anyway?</p>