Sockets secure implementations

Rebased on `master`.
We need to wait for the receive events. We have a `closeAllSockets` method to close all pending sockets. We also close sockets when receive events raise.