title: Accept() in a thread to avoid "blocking" socket
body: <p>I'd like to handle the <code>accept()</code> method in a separate thread, to avoid the general freeze while it waits for a connection.</p>
  
  <p>The code (*<em>server only! *</em>):</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
    int sockfd, newsockfd, portno;
    socklen_t clilen;
    char buffer[256];
    struct sockaddr_in serv_addr, cli_addr;
    int n;
  
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
  
    bzero((char *) &amp;serv_addr, sizeof(serv_addr));
    portno = atoi(argv[1]);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
  
    bind(sockfd, (struct sockaddr *) &amp;serv_addr, sizeof(serv_addr));
  
    listen(sockfd,5);
    clilen = sizeof(cli_addr);
    newsockfd = accept(sockfd, (struct sockaddr *) &amp;cli_addr, &amp;clilen);
  
    bzero(buffer,256);
    n = read(newsockfd,buffer,255);
  
    n = write(newsockfd,"Message : ",18);
  
    close(newsockfd);
    close(sockfd);
    return 0; 
  }
  </code></pre>
  
  <p>How can I create a separate thread within this code, preventing the <code>accept()</code> call to freeze the program. As a bonus, I'd like to handle multiple <code>accept()</code> (so the socket does not close itseld after the message is received, but continues to listen and accept requests).</p>