title: Async Send to multiple clients and correct usage of FromAsync?
body: <p>I have 500 connected clients on the server. Server sends data to all clients in every N sec.</p>
  
  <p>When the server sends data to clients the result is:</p>
  
  <pre><code>-----------Result-----------
  Client1 received : 102 datas
  Client2 received : 109 datas
  Client3 received : 105 datas
  Client4 received : 108 datas
  Client5 received : 108 datas
  ClientN received : 107 datas
  </code></pre>
  
  <p>But it should be 110 datas at the end.</p>
  
  <p>I am using the following code to send data to clients</p>
  
  <pre><code>public static class AsyncSend
  {
      public static Task&lt;int&gt; SendAsync(this Socket socket, byte[] buffer, int offset, int count)
      {
          return new Task&lt;int&gt;(() =&gt;
          {
              return Task.Factory.FromAsync&lt;int&gt;(
                             socket.BeginSend(buffer, offset, count, SocketFlags.None, null, socket),
                       socket.EndSend).Result;
          });
      }
  }
  </code></pre>
  
  
  
  <pre><code>class ServerTest
  {
    private Thread sendThread;
    private Dictionary&lt;String, Client&gt; connectedClients;
  
    public ServerTest()
    {
      connectedClients = new Dictionary&lt;String, Client&gt;();
    }
  
    public void receive()
    {
      //here server receives data from another server
      //When server receives new data then fires the newDataReceived(String data) method.
  
      newDataReceived(data);
    }
  
    public void newDataReceived(String data)
    {
      sendThread = new Thread(new ParameterizedThreadStart(sendToClients));
      sendThread.Start(data);
    }
  
    private void sendToClients(object dataObj)
    {
      try
      {
        int totalConnectedClients = connectedClients.Count;
  
        if (totalConnectedClients &gt; 0)
        {
          lock (connectedClients)
          {
            byte[] buffer = Encoding.UTF8.GetBytes((string)dataObj);
            byte[] cBuffer = new SCompressor().Compress(buffer);
  
            foreach (Client client in connectedClients.Values)
            {
              Task&lt;int&gt; task = AsyncSend.SendAsync(client.Socket, cBuffer, 0, cBuffer.Length);
              task.Start();
            }
          }
        }
      }
      catch (Exception ex)
      {
        //ERROR.
      }
    }
  </code></pre>
  
  <p>Is there another way or how to send data to multiple clients correctly and efficiently?</p>
  
  <p>Please show me examples if you can.</p>
  
  <p>Thanks in advice.</p>