Bug fixes and missing import

Missing import of socket module for the exception ``socket.error``.

Method ``connected()`` returns ``False`` when ``self.connection`` doesn't exist.