On some remote machines my SSH connections hangs if they idle for a while. To get around that you can add a flag to send a keepalive signal reularly. Just open your ~/.ssh/config and add the following for the host that hangs:
Host example.com ServerAliveInterval 60
Or to enable it for all hosts:
Host * ServerAliveInterval 60
60 is the time in seconds between keepalive packages. Adjust this as needed.