Sockets secure implementations

Rebased on `master`.
Nit: Is compact() necessary here? Probably just causes an unnecessary copy...