A connect failed: ECONNREFUSED (Connection refused) most likely means that there is nothing listening on that port AND that IP address. Possible explanations include:
- the service has crashed or hasn’t been (successfully!) started,
- your client is trying to connect using the wrong IP address or port,
- your client is trying to connect using a DNS name that resolves to the wrong IP, or
- server access is being blocked by a firewall that is “refusing” on the server/service’s behalf. This is pretty unlikely given that normal practice (these days) is for firewalls to “blackhole” all unwanted connection attempts.
It is also possible that you are using a local proxy server when you shouldn’t be … or vice versa. This can happen if you are using a corporate network where access to external networks is restricted or monitored. Check with your local network team and/or check that your proxy settings are correct.
Note that while you have an array variable called urls, it cannot contain real URLs. There is no overload of the Socket constructor that takes a real URL in any form. Indeed, if you supplied a URL in string form like this:
new Socket("http://example.com", 42)
the result would be a different exception.
Likewise, if you attempt to connect to an IP address on a network that you can’t route to (e.g. “a different WiFi network”), then you will get a different exception; e.g. “host not found”, “no route to host” or “no route to network”.