Slow TcpClient Connection (sockets)

Recently I have been experimenting with Sockets and trying to communicate with a windows service both with a console app and a website.

After managing to get some lines of communication going between the client and server applications, I couldn’t help but notice that it was taking a little bit longer than it probably should have. I decided to have a closer look.

Using some basic timestamping in the console/website application the whole process was taking just over 1 second. This was to connect, send and then receive a response. Further investigation revealed only 1 statement was causing the time to blow out. Below is the statement.

TcpClient socketForServer = new TcpClient("localhost", 200);

Now in all the demos and tutorials this is how it showed to connect to the server. Pretty straightforward and simple. Or so I thought. Whilst having a look through the methods associated with the TcpClient, there was one that stood out: Connect(). Hmmm…this also took a host name and a port. I decided to give it a try.

socketForServer = new TcpClient(); socketForServer.Connect("localhost", 200);

I bet you’re wondering right now. What is the difference?? At this point in time I’m not quite sure but I’m determined to figure it out because after making that change the time to execute the previous two statements was about 1 millisecond or so. Much better.

One thing that did seem odd was if the TcpClient failed to make a connection, the response time was almost exactly the same (~1sec).

If anyone knows the reason, please leave a comment. Until then, two lines will have to do.

This entry was posted in .NET and tagged , , . Bookmark the permalink.

4 Responses to Slow TcpClient Connection (sockets)

  1. Mike Koss says:

    I found your post trying to debug a local Python web server I’m running. Requests were also taking about 1 second each, when they should be nearly instantaneous.

    One change I made, connect to “” insead of “localhost” -> now everything is fast again!

    Is Vista doing a DNS call (and timing out), before connecting to the local machine???

  2. Schotime says:


    This is also related to the slow response from firefox when running on localhost with vista.

    The reason for it is because of the ipv6 protocol. In the hosts file of your pc under vista there is two entries for localhost. One for ipv6 and one for ipv4. I have now just commented out the ipv6 one with a # and everything is as fast as it should be now (<4ms).

    Its obviously taking 1 second to decide which protocol to use or gets confused. Something….whatever. haha. Least it works now.

    Hope this helps everyone.
    Thanks for the comments Mike.

  3. Pedro Duarte says:

    I had the same problem with Windows Vista and 7, and with this solution it works very fast.

  4. Jason Smith says:

    Using the TcpClient() constructor with no parameters works only with ipV4 which is why you are noticing this difference.

    See here:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s