Optionally enable keepalive on the socket

Keepalive actively checks that the other end of the connection is still
open, and prevents blocking forever on half-open connections.
Merge pull request #187 from arnaud-lb/keepalive

Optionally enable keepalive on the socket