Socket changes

I've applied the last commits since we forked this library specially to solve a bug we're having not closing the socket if the connection fails.
?