Added error handling for sockets.

Signed-off-by: Borja Tarraso <borja.tarraso@member.fsf.org>