If you are connecting to a remote system, it could be NAT configured badly on your router.
The router provided by my ISP (Virgin Media) is ruthless at closing idle TCP connections after only a few minutes. I'd see this with idle SSH logins being closed all the time.
The solution (for me at least) was to ensure connections used TCP keepalives, and vastly decrease the keepalive times (various sysctl calls, I don't have the details to hand).
But instead required a bunch of moderately obscure changes to system config which you are bound to forget after a few years when you reinstall / image a new machine. The Virgin Superhub is a crappy barely-consumer grade box from Netgear with firmware written by Virgin. Modem mode is all it's good for. Sometimes the right answer is to spend the money on a decent router - Draytek are passable.
The router provided by my ISP (Virgin Media) is ruthless at closing idle TCP connections after only a few minutes. I'd see this with idle SSH logins being closed all the time.
The solution (for me at least) was to ensure connections used TCP keepalives, and vastly decrease the keepalive times (various sysctl calls, I don't have the details to hand).