title: C# TcpClient cannot connect
body: <p>I create a class which connects with other computer in LAN. When I induce a method Connect first time all is ok, but after work class the variable lose a reference to this object. </p>
  
  <p>But if I try do create this object again and I induce a method Connect I have an error which is saying <code>"Only one using each socket adress(protocol / network address / port) is permitted"</code> </p>
  
  <p>this error is when I try to do </p>
  
  <p><code>partner.Connect(partnerIPEndPoint);</code>. </p>
  
  <p>What is bad in my class? I would appreciate if anybody could help me.
  Thanks in advance.</p>
  
  <p>Here is my class:</p>
  
  <pre><code>using System;
  using System.Net;
  using System.Net.Sockets;
  using System.Diagnostics;
  
  
  namespace AudioNetwork
  {
      class ConnectToServer : IDisposable
      {
          public int BeepAmonut { get; set; }
          public int Speed { get; set; }
          public IPAddress MyIPAddress { get; set; }
          public IPAddress PartnerIPAddress { get; set; }
          private NetworkStream stream;
          private TcpClient partner;
  
  
          public void Connect()
          {
             IPEndPoint myIPEndPoint = new IPEndPoint(MyIPAddress, 800);
             IPEndPoint partnerIPEndPoint = new IPEndPoint(PartnerIPAddress, 800);
             partner = new TcpClient(myIPEndPoint);              
  
             partner.Connect(partnerIPEndPoint);
             stream = partner.GetStream();
             Messanger(stream);
          }
  
          protected virtual void Messanger(NetworkStream myStream)
          {
              byte[] data = { 2, 1 };
  
              myStream.Write(data, 0, data.Length);
              Play(2000);
  
          }
  
          public void Play(int time)
          {
              Stopwatch sw = new Stopwatch();
              sw.Start();
              while (sw.ElapsedMilliseconds &lt;= 2100)
              {
                  if (sw.ElapsedMilliseconds &gt;= time)
                  {
                      break;
                  }
              }
          }
  
          public void Dispose()
          {
              GC.SuppressFinalize(this);
              stream.Dispose();
              stream.Close();
              partner.Client.Dispose();
              partner.Close();
          }
      }
  
      class ConnectToClient : ConnectToServer
      {
          protected override void Messanger(NetworkStream myStream)
          {
              byte[] data = new byte[256];
              int bytes = myStream.Read(data, 0, data.Length);
              if (data[0] == 2)
              {
                  Play(1980);
              }
          }
      }
  }
  </code></pre>