title: >1 client can't connect to server in c#
body: <p>I am creating a chat client in C# to be demonstrated on localhost. </p>
  
  <p>Here's the relevant code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using System.Net.Sockets;
  using System.Net;
  using System.IO;
  using System.Threading;
  
  namespace WindowsFormsApplication1
  {
   public partial class Form1 : Form
   {
      static List&lt;TcpListener&gt; garabage_collection_preventor = new List&lt;TcpListener&gt;();
      static Dictionary&lt;IPEndPoint, bool&gt; address_dictionary = new Dictionary&lt;IPEndPoint, bool&gt;();
      static int port_increment = 9999;
      static int client_id = 0;
  
      void start_listening()
      {
          while (true)
          {
              TcpListener listen = new TcpListener(IPAddress.Any, port_increment);
              garabage_collection_preventor.Add(listen);
              listen.Start();
              TcpClient client = listen.AcceptTcpClient();
              IPEndPoint temp_end = ((IPEndPoint)listen.LocalEndpoint);
              address_dictionary.Add(temp_end, false);
              port_increment++;
              new Thread(new ParameterizedThreadStart(connection_stuff)).Start(client);
          }
      }
  
      void writer(object ob,int end_point)
      {
          StreamWriter write = ob as StreamWriter;
          while (true)
          {
              foreach (KeyValuePair&lt;IPEndPoint, bool&gt; value in address_dictionary)
              {
                  IPEndPoint index = value.Key;
                  int temp = value.Key.Port;
                  if (temp == end_point)
                  {
                      while (address_dictionary[index] == true)
                      {
                          write.WriteLine(msg_box.Text);
                      }
                  }
              }
          }
      }
  
      void reader(StreamReader read)
      {
          while (true)
          {
              MessageBox.Show(read.ReadLine());
          }
      }
  
      void connection_stuff(object ob)
      {
          TcpClient client = ob as TcpClient;
          int writer_identification_endpoint = ((IPEndPoint)client.Client.LocalEndPoint).Port;
  
          NetworkStream stream = client.GetStream();
          StreamReader read = new StreamReader(stream);
          StreamWriter write = new StreamWriter(stream);
  
          ThreadStart port_passing = delegate { writer(write, writer_identification_endpoint); };
          Thread thread = new Thread(port_passing);
  
          reader(read);
          thread.Start();
      }
  
      public Form1()
      {
          InitializeComponent();
      }
  
      private void send_Click(object sender, EventArgs e)
      {
          int end_point = int.Parse(port.Text);
          foreach (KeyValuePair&lt;IPEndPoint, bool&gt; value in address_dictionary)
          {
              IPEndPoint index = value.Key;
              int temp = value.Key.Port;
              if (temp == end_point)
              {
                  address_dictionary[index] = true;
              }
          }
      }
  
      private void listener_Click(object sender, EventArgs e)
      {
          new Thread(start_listening).Start();
          listener.Enabled = false;
      }
   }
  }
  </code></pre>
  
  <p>Now the problem is that the first client can easily connect with the program and send messages which this program can easily read. However every subsequent client cannot connect. </p>
  
  <p>I know that I shouldn't be making creating than one TCPListener but the problem is that I have to demonstrate the program on localhost and port number is the only true way to differentiate between client. </p>
  
  <p>So please tell me what's wrong with the code I have been banging my head against the wall on it for hours.</p>
  
  <p>EDIT</p>
  
  <p>this is what happens when english is not a first language :)  This code(not complete presently) will be a chat client. Each instance of this code will be able to connect with other instance of this same code to communicate. Any number of instances should be able to connect with any number of instances(like if double click 5 times the program there would now be 5 instances ready to communicate with each other).</p>
  
  <p>Now the problem is that every instance will have the same IP Address(Because they are all running on the same machine). The question arises how say instance 1 suppose to connect to instance 4, ip can't be used here because instance 2,3 and 5 will also have the same IP address. So what i am trying to do is to connect instance 1 with instance 4 with IP Address and PORT instead of just using just the IP Address as is the case with a single TCPListener.</p>