title: Node.js TCP/IP server queue not working
body: <p>I'm writing a TCP/IP queue that's supposed to limit maximum connected users to 3 and make any others wait until user disconnects. These are the functions I'm using:</p>
  
  <pre><code>var maxClients = 3;
  var currentClients = 0;
  var _pending = [];
  
  function process_pending()
  {
    if (_pending.length &gt; 0) {
      var client = _pending.shift();
      currentClients++;
      client(function() 
      {
        currentClients--;
        process.nextTick(process_pending);
      });
    }
  }
  
  function client_limit(client) 
  {
    if (currentClients &lt; maxClients) {
      currentClients++;
      client(function() 
      {
        currentClients--;
        process.nextTick(process_pending);
      });
    }
    else 
    {
      console.log('Overloaded, queuing clients...');
      _pending.push(client);
    }
  }
  </code></pre>
  
  <p>Whenever three clients are connected and then fourth one tries to connect it displays the Overloaded message and that's okay, but if an existing client disconnects, nobody else can connect. Looks like the </p>
  
  <pre><code>client(function() 
          {
            currentClients--;
            process.nextTick(process_pending);
          });
  </code></pre>
  
  <p>Doesn't work properly, how do I fix it?</p>