title: Scaling Connections with BlockingCollection<T>()
body: <p>I have a server which communicates with 50 or more devices over TCP LAN.  There is a Task.Run for each socket reading message loop.  </p>
  
  <p>I buffer each message reach into a blocking queue, where each blocking queue has a Task.Run using a BlockingCollection.Take().</p>
  
  <p>So something like (semi-pseudocode):</p>
  
  <p><strong>Socket Reading Task</strong></p>
  
  <pre><code>Task.Run(() =&gt;
  {
      while (notCancelled)
      {
          element = ReadXml();
          switch (element)
          {
              case messageheader:
                  MessageBlockingQueue.Add(deserialze&lt;messageType&gt;());
              ...
          }
      }
  });
  </code></pre>
  
  <p><strong>Message Buffer Task</strong></p>
  
  <pre><code>Task.Run(() =&gt;
  {
      while (notCancelled)
      {
          Process(MessageQueue.Take());
      }
  });
  </code></pre>
  
  <p>So that would make 50+ reading tasks and 50+ tasks blocking on their own buffers.</p>
  
  <p>I did it this way to avoid blocking the reading loop and allow the program to distribute processing time on messages more fairly, or so I believe.</p>
  
  <p>Is this an inefficient way to handle it? what would be a better way?</p>