fatal: Read from socket failed: Connection reset by peer

I get this error when I log in through console:
"fatal: Read from socket failed: Connection reset by peer".

Can you tell me what this is and why it happens, and how to stop it? Thank you.

The problem is external to your code.

Assuming a SOCKSTREAM:
It means the connection was forcibly closed by a peer. It a loss of the connection to the remote socket due to a timeout or a restart, the remote process died, a network component died, etc. In TCP terms, the remote host or maybe an intervening component sent an RST packet.

In anthroplogical terms it means the remote host "wants" you to try to connect and then send the packet again. If it was hardware or a process crash you probably won't reconnect but you should try. listen() with a time out, or try a connect() - which ever is appropriate.