title: How can I make my server multithreaded?
body: <p>I have a WinSock server and I was wondering how to make it multi-threaded. I have used threads before and I understand what they do. But I'm not sure how to use them with my server code. So here it is:</p>
  
  <p>main.cpp</p>
  
  <pre><code>/* This is our Server */
  
  //Includes
  #include "Functions.h"
  
  //Main function
  int main(){
      DLLVERSION = MAKEWORD(2,1);
      answer = WSAStartup(DLLVERSION, &amp;wsaData);
      sConnect = socket(AF_INET, SOCK_STREAM, NULL);
      addr.sin_addr.s_addr = inet_addr(HostIP);
      addr.sin_family = AF_INET;
      addr.sin_port = htons(PORT);
      sListen = socket(AF_INET, SOCK_STREAM, NULL);
      bind(sListen, (SOCKADDR*)&amp;addr, sizeof(addr));
      listen(sListen, SOMAXCONN);
  
      //Listening for clients
      for(;;){
          Print("Waiting for incoming connections");
          //If a connection is found
          if(sConnect = accept(sListen, (SOCKADDR*)&amp;addr, &amp;addrlen)){
              Print("Connection found!");
              Send("Hello!");
          }
      }
  }
  </code></pre>
  
  <p>How would I make this server multithreaded?</p>
  
  <p>I tried using <code>CreateThread();</code> but as soon as another thread is created, the last thread ends for some reason. Can anyone help?</p>