title: C# Socket Programming: server does not accept client connections
body: <p>I am new to socket programming in C#. Have searched the web like crazy for the solution to my problem but didnt find anything that could fix it. So heres my problem:</p>
  <p>I am trying to write a client-server application. For the time being, the server will also run on my local machine. The application transmits a byte stream of data from the client to the server. <strong>The problem is that the server doesnt detect a client request for connection, while the client is able to connect and transmit the byte stream</strong>.</p>
  <p>here is the server code:</p>
  <pre><code>String strHostName = Dns.GetHostName();
  Console.WriteLine(strHostName);
  IPAddress ip = IPAddress.Parse(&quot;127.0.0.1&quot;);
  ipEnd = new IPEndPoint(ip, port);
  soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
  soc.Bind(ipEnd);
  
  Console.WriteLine(&quot;Web Server Running... Press ^C to Stop...&quot;);
  
  Thread th = new Thread(new ThreadStart(StartListen));
  th.Start();
  </code></pre>
  <p>The StartListen thread is as below:</p>
  <pre><code>public void StartListen()
  {
      try
      {
          while (true)
          {
              string message;
              Byte[] bSend = new Byte[1024];
              soc.Listen(100);
  
              if (soc.Connected)
              {
                  Console.WriteLine(&quot;\nClient Connected!!\n==================\n CLient IP {0}\n&quot;, soc.RemoteEndPoint);
                  Byte[] bReceive = new Byte[1024 * 5000];
                  int i = soc.Receive(bReceive);
  </code></pre>
  <p>The client code is as follows:</p>
  <pre><code>hostIPAddress = IPAddress.Parse(&quot;127.0.0.1&quot;);
  ipEnd = new IPEndPoint(hostIPAddress,port);
  Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
  
  Console.WriteLine(&quot;Connecting to Server...&quot;);
  clientSocket.Connect(ipEnd);
  Console.WriteLine(&quot;Sending File...&quot;);
  clientSocket.Send(clientData);
  Console.WriteLine(&quot;Disconnecting...&quot;);
  clientSocket.Close();
  Console.WriteLine(&quot;File Transferred...&quot;);
  </code></pre>
  <p>Now what happens is that the server starts and when I run the client, it connects, sends and closes. But nothing happens on the server console, it doesnt detect any connection: <strong>if (soc.Connected) remains false</strong>.</p>
  <p>I checked whether the server was listening to 127.0.0.1:5050 through netstat, and it sure was listening. Cant figure out the problem. Please help.</p>