title: 'Connection refused' connecting through TCP/IP
body: <p>I want to connect unity app on android with a computer application. I could run it on the network but over internet it is always saying the machine is refusing the connection. I have disabled the firewall of windows and anti-virus but still have problem connecting the server and Android client:</p>
  
  <pre><code>No connection could be made because the target machine actively refused it.
  </code></pre>
  
  <p>Here is the code:</p>
  
  <p>Server side:</p>
  
  <pre><code>TcpListener tcpListener;
  Socket socket;
  NetworkStream networkStream;
  Thread thread;
  
  string getIpAddress()
  {
      System.Net.IPHostEntry host;
      string localIp = "";
      host = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
      foreach (System.Net.IPAddress ip in host.AddressList)
      {
          localIp = ip.ToString();
      }
      return localIp;
  }
  
  public void ReceiveImage()
  {
      try
      {
          IPAddress localAddr = IPAddress.Parse("95.171.54.53");
          tcpListener = new TcpListener(localAddr, 53100);
          tcpListener.Start();
          socket = tcpListener.AcceptSocket();
          networkStream = new NetworkStream(socket);
          pictureBox1.Image = Image.FromStream(networkStream);
          if (socket.Connected == true)
          {
              while (true)
              {
                  tcpListener.Stop();
                  ReceiveImage();
              }
          }
      }
      catch(Exception ex)
      {
          string Message = ex.Message;
      }
  }
  
  private void Form1_Load(object sender, EventArgs e)
  {
      thread = new Thread(new ThreadStart(ReceiveImage));
      thread.Start();
  }
  
  private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  {
      tcpListener.Stop();
      thread.Abort();
  }
  </code></pre>
  
  <p>This is the client side:</p>
  
  <pre><code>// Update is called once per frame
  int frameSaveCount = -1;
  int FrameCount = -1;
  Socket socket;
  NetworkStream networkStream;
  Thread thread;
  MemoryStream memoryStream;
  TcpClient tcpClient;
  BinaryWriter binaryWriter;
  
  void Update()
  {
      // ...
      if(FrameCount % 10==0)
      {
          // ...
          send(bytes);
      }
  }
  
  void send(Byte[] bytes)
  {
      try
      {
          print(getIpAddress());
          tcpClient = new TcpClient("95.171.54.53", 53100);
          networkStream = tcpClient.GetStream();
          binaryWriter = new BinaryWriter(networkStream);
          binaryWriter.Write(bytes);
          binaryWriter.Close();
          networkStream.Close();
          tcpClient.Close();
      }
      catch (Exception exception)
      {
          string message = exception.Message;
          print(message);
      }
  }
  </code></pre>