Sockets secure implementations

Rebased on `master`.
why not close sockets?