def _connect(resolved_address, **config):
s = None<EOL>try:<EOL><INDENT>if len(resolved_address) == <NUM_LIT:2>:<EOL><INDENT>s = socket(AF_INET)<EOL><DEDENT>elif len(resolved_address) == <NUM_LIT:4>:<EOL><INDENT>s = socket(AF_INET6)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>".format(resolved_address))<EOL><DEDENT>t = s.gettimeout()<EOL>s.settimeout(config.get("<STR_LIT>", DEFAULT_CONNECTION_TIMEOUT))<EOL>log_debug("<STR_LIT>", resolved_address)<EOL>s.connect(resolved_address)<EOL>s.settimeout(t)<EOL>s.setsockopt(SOL_SOCKET, SO_KEEPALIVE, <NUM_LIT:1> if config.get("<STR_LIT>", DEFAULT_KEEP_ALIVE) else <NUM_LIT:0>)<EOL><DEDENT>except SocketTimeout:<EOL><INDENT>log_debug("<STR_LIT>", resolved_address)<EOL>log_debug("<STR_LIT>", resolved_address)<EOL>s.close()<EOL>raise ServiceUnavailable("<STR_LIT>".format(resolved_address))<EOL><DEDENT>except (IOError, OSError) as error:  <EOL><INDENT>log_debug("<STR_LIT>", type(error).__name__, "<STR_LIT:U+0020>".join(map(repr, error.args)))<EOL>log_debug("<STR_LIT>", resolved_address)<EOL>s.close()<EOL>raise ServiceUnavailable("<STR_LIT>".format(resolved_address, error))<EOL><DEDENT>else:<EOL><INDENT>return s<EOL><DEDENT>
:param resolved_address:
:param config:
:return: socket object