SocketException: Connection Refused - but can connect with other programs

I have a Unity script that connects to a TCP port. When it is connecting to localhost, it works, but when connecting to a remote ip on the local network it receives “SocketException: Connection Refused” as an error.

Typically, the cause of this is a firewall rule on the host or the client. But I ruled this out by running a curl to the port on the remote host and it works. I also verified this by running WireShark and capturing the exchanged network packets.

Any ideas? I am running Unity 2019.4.17f1 Personal on MacOS.

Sounds like software firewall settings on the computer acting as the host.

In MacOS - at least when creating a sandboxed Application with XCode - you need to set up the correct entitlements for your app in your plist (I think it’s something like com.apple.security.network.client)

I have no idea how unity handles that but looking at the macOS specific settings may help (if Unity’s apps aren’t sandboxed, this probably does not apply).

I figured it out. The external IP was being over-written by another component so it was trying to connect to localhost which was incorrect. Thank you all the same for the help.