You would usually run into this when attempting to use connect(2) a socket(2). "Connection refused" means that the remote host rejected your connection attempt. This usually means that the host was not listening on the port you attempted to connect on, or there is a firewall preventing you from connecting to that port.
You can get this on connectionless protocols such as UDP if the protocol returned an ICMP error message such as DestinationPortUnreachable. Note that because UDP is connectionless the nexttime you send(2)/recv(2) a packet on the same socket you may get the error, not immediately when you send the packet. Thus if you get this error from send(2) (or recv(2)) then it is likely that the packet you just sent was fine, but one of the packets you sent earlier returned an error message, however it's impossible to tell which packet exactly it was (unless you open a new socket per packet...)
7 pages link to ECONNREFUSED: