A: 

Is it always the same webservice you connect to or could it be that one webservice works but another is down or is blocked by a firewall or similar?

ho1
A: 

Most possible reason is a Firewall.

This article contains a set of reasons. It may be useful to you.

From the article the reasons would be,

FTP server settings
Software/Personal Firewall Settings
Multiple Software/Personal Firewalls
Anti-virus Software
LSP Layer
Router Firmware
Chathuranga Chandrasekara
+2  A: 

If this happens always, it literally means that the machine exists but that it has no services listening on the specified port, or there is a firewall stopping you.

If it happens occasionally - you used the word "sometimes" - and retrying succeeds, it is likely because the server has a full 'backlog'.

When you are waiting to be accepted on a listening socket, you are placed in a backlog. This backlog is finite and quite short - values of 1, 2 or 3 are not unusual - and so the OS might be unable to queue your request for the 'accept' to consume.

The backlog is a parameter on the listen function - all languages and platforms have basically the same API in this regard, even the C# one. This parameter is often configurable if you control the server, and is likely read from some settings file or the registry. Investigate how to configure your server.

If you wrote the server, you might have heavy processing in the accept of your socket, and this can be better moved to a separate worker-thread so your accept is always ready to receive connections. There are various architecture choices you can explore that mitigate queuing up clients and processing them sequentially.

Regardless of whether you can increase the server backlog, you do need retry logic in your client code to cope with this issue - as even with a long backlog the server might be receiving lots of other requests on that port at that time.

There is a rare possibility where a NAT router would give this error should it's ports for mappings be exhausted. I think we can discard this possibility as too much of a long shot though, since the router has 64K simultaneous connections to the same destination address/port before exhaustion.

Will
Yes I get it occasionally, not always, I dont think reason is firewall or anything else, but backlog can be the reason of this issue. Is there anyway to solve this issue ?
hsnkvk
@hsnkvk; the proper robust way is for the client to have a retry loop; answer updated accordingly
Will
@Will; I already have an iterationCount and i retry iterationCount times if it fails. :(
hsnkvk
@hsnkvk - its an exception, you should catch it and retry some number of times, perhaps after some short delay
Will
@Will, even I am having the same problem. One webserver. Two web applications. Both have webservices. Every minute my clients independently talk to each webservice. The webservice in the first web app gives no trouble at all. But the webservice in the second webapp is nothing but trouble. It randomly throws the Connection refused error that OP mentioned. I catch the exception and retry after a minute. But its completely random. Sometimes, it wont work for days at a stretch even if I restart the client/web-server. Any pointers as to how can I debug this?
Amith George
Btw, should I create a new SO question, should we continue this in the comments?
Amith George
Well I don't have much input other than what I said above, which might be worth an upvote ;)<br/>The second webapp seems to be badly designed/configured. But without code inspection its hard to second guess.
Will
Thats the thing. Badly designed/configured is rather vague as you said. By code inspection, do you mean you want to view the code that calls the webservice? I didnt post it as its the exact same code as the one that calls the first webservice and that one works perfect. Is there any property on the server that can be configured via web.config which might affect the second webapp?
Amith George
I don't want to view your code; at this point, it might be worth you calling in a contractor
Will
umm, Ok. Thanks for your time :)
Amith George