title: TcpClient creation seem to be very slow. Can I cache those?
body: <p>I have program where client peers communicate with each other via TCP-IP. When one client does something he will signal other clients one by one that this happened. Here is a code I use to send data across:</p>
  
  <pre><code>public static string SendDirect(string data, string hostName, int portNumber)
          {
              string responseData;
  
              try
              {
                  var client = new TcpClient(hostName, portNumber);
  
                  Stream s = client.GetStream();
                  var sw = new StreamWriter(s) { AutoFlush = true };
  
                  sw.WriteLine(data);
  
                  s.Close();
                  client.Close();
  
                  s.Dispose();
                  sw.Dispose();
  
                  responseData = "OK";
              }
              catch (SocketException ex)
              {
                  responseData = ex.Message;
              }
  
              return responseData;
          }
  </code></pre>
  
  <p>Line</p>
  
  <blockquote>
    <p>var client = new TcpClient(hostName,
    portNumber);</p>
  </blockquote>
  
  <p>can be very slow at times for some machines. For example, in my home network it takes like 2 or 3 seconds. Can you see how it's real bad with 15 clients.</p>
  
  <p>I was wondering how expensive or if even possible to not Close client every time and keep 30-40 of them open at all times? I assume some mechanism to check to make sure they alive and to make sure they all closed properly need to be coded but I wonder if idea itself is correct..</p>
  
  <p>Thanks!</p>