title: Asynchronous winsock server wrapper, CPU lagging - C++
body: <p>I am trying to write a Server application wrapper, as I would with any application and I've searched for over a week for a at least decent guide or tutorial on asynchronous sockets( this wrapper has to be asynchronous ) and so far what i could do is this:</p>
  
  <pre><code>#ifndef _SERVER_H
  #define _SERVER_H
  
  #include "asynserv.h" // header file with the important lib includes
  #include &lt;map&gt;
  
  namespace Connections
  {
      DWORD WINAPI MainThread(LPVOID lParam); // Main thread
      DWORD WINAPI DataThread(LPVOID lParam); // thread that will be created for each client
      struct ClientServer // struct to keep a server and a client pair.
      {
      public:
          struct Client* Client;
          class Server* Server;
      };
      struct Client // a struct wrapper to keep clients
      {
      public:
          Client(SOCKET Connection, int BufferSize, UINT ID);
          ~Client();
          SOCKET WorkerSocket;
          char Buffer[255];
          bool Connected;
          int RecvSize;
          UINT UID;
          void Send(char * Data);
          void Disconnect();
      };
      class Server
      {
      private:
          SOCKET WorkerSocket;
          SOCKADDR_IN EndPnt;
          UINT ID;
          int CBufferSize;
      public:
          Server(int Port, int Backlog, int BufferSize);
          ~Server();
          __event void ClientRecieved(Client* Clientr, char * RecData);
          bool Enabled;
          int Port;
          int Backlog;
          HWND ReqhWnd;
          std::map&lt;UINT, Client*&gt; ClientPool;
          void WaitForConnections(Server*);
          void WaitForData(Client*);
          void InvokeClientDC(UINT);
          void Startup();
          void Shutdown();
      };
  }
  #endif
  </code></pre>
  
  <p>Server.cpp:</p>
  
  <pre><code>#include "Server.h"
  
  namespace Connections
  {
      void Server::Startup()
      {
          WSADATA wsa;
          WSAStartup(0x0202, &amp;wsa);
          this-&gt;WorkerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
          this-&gt;EndPnt.sin_addr.s_addr = ADDR_ANY;
          this-&gt;EndPnt.sin_family = AF_INET;
          this-&gt;EndPnt.sin_port = htons(this-&gt;Port);
          this-&gt;Enabled = true;
          this-&gt;ID = 0;
          bind(this-&gt;WorkerSocket, (SOCKADDR*)&amp;this-&gt;EndPnt, sizeof(this-&gt;EndPnt));
          printf("[AsynServer]Bound..\n");
          listen(this-&gt;WorkerSocket, this-&gt;Backlog);
                  CreateThread(NULL, NULL, &amp;MainThread, this, NULL, NULL);
      }
      void Server::WaitForConnections(Server * Ser)
      {
          WSAEVENT Handler = WSA_INVALID_EVENT;
          while(Ser-&gt;Enabled)
          {
              Handler = WSACreateEvent();
              WSAEventSelect(Ser-&gt;WorkerSocket, Handler, FD_ACCEPT);
              WaitForSingleObject(Handler, INFINITE);
              SOCKET accptsock = accept(Ser-&gt;WorkerSocket, NULL, NULL);
              Client * NewClient = new Client(accptsock, 255, Ser-&gt;ID++);
              NewClient-&gt;Connected = true;
              printf("[AsynServer]Client connected.\n");
              ClientServer * OurStruct = new ClientServer();
              OurStruct-&gt;Server = Ser;
              OurStruct-&gt;Client = NewClient;
              CreateThread(NULL, NULL, &amp;DataThread, OurStruct, NULL, NULL);
          }
      }
      void Server::WaitForData(Client * RClient)
      {
          WSAEVENT Tem = WSA_INVALID_EVENT;
          Tem = WSACreateEvent();
          WSAEventSelect(RClient-&gt;WorkerSocket, Tem, FD_READ);
          while(RClient-&gt;Connected)
          {
              WaitForSingleObject(Tem, INFINITE);
              RClient-&gt;RecvSize = recv(RClient-&gt;WorkerSocket, RClient-&gt;Buffer, 255, NULL);
              if(RClient-&gt;RecvSize &gt; 0)
              {
                  RClient-&gt;Buffer[RClient-&gt;RecvSize] = '\0';
                  __raise this-&gt;ClientRecieved(RClient, RClient-&gt;Buffer);
                  Sleep(50);
              }
          }
          return;
      }
      DWORD WINAPI MainThread(LPVOID lParam)
      {
          ((Server*)lParam)-&gt;WaitForConnections((Server*)lParam);
          return 0;
      }
      DWORD WINAPI DataThread(LPVOID lParam)
      {
          ClientServer * Sta = ((ClientServer*)lParam);
          Sta-&gt;Server-&gt;WaitForData(Sta-&gt;Client);
          return 0;
      }
  }
  </code></pre>
  
  <p>Now after creating the server instance and creating the main thread, i can accept clients simultaneously and read data they send, but after TWO connections my CPU lags up till 100% usage..
  I guess my method is incorrect, so my question is can someone point out a possible flaw in my code, or just point me out a decent guide for asynchronous sockets, provided that i have already searched for over a week with no results( probably my despair is hindering me from choosing the correct keywords :| ).
  Thanks in advance and sorry about the huge piece of code, trimmed it as long as it allowed.</p>
  
  <p>Mfg,
  SimpleButPerfect.</p>