Sockets secure implementations

Rebased on `master`.
Why not close sockets at the end?