client: add a defer call to ensure connection termination with sockets

Closes #116 (thanks @gravis).