'Ensure a socket exists and is appropriately configured.
This function is called on start up, and can also be
called in the event of a configuration reload.
When called for the first time a new socket is created.
If reloading and either bind_host or bind port have been
changed the existing socket must be closed and a new
socket opened (laws of physics).
In all other cases (bind_host/bind_port have not changed)
the existing socket is reused.
:param old_conf: Cached old configuration settings (if any)
:param has changed: callable to determine if a parameter has changed'

def configure_socket(self, old_conf=None, has_changed=None):

    new_sock = ((old_conf is None) or (has_changed('bind_host') or has_changed('bind_port')))
    use_ssl = (not ((not self.conf.cert_file) or (not self.conf.key_file)))
    old_use_ssl = ((old_conf is not None) and (not ((not old_conf.get('key_file')) or (not old_conf.get('cert_file')))))
    wrap_sock = ((use_ssl is True) and ((old_use_ssl is False) or new_sock))
    unwrap_sock = ((use_ssl is False) and (old_use_ssl is True))
    if new_sock:
        self._sock = None
        if (old_conf is not None):
            self.sock.close()
        _sock = get_socket(self.conf, self.default_port)
        _sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        _sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
        self._sock = _sock
    if wrap_sock:
        self.sock = ssl.wrap_socket(self._sock, certfile=self.conf.cert_file, keyfile=self.conf.key_file)
    if unwrap_sock:
        self.sock = self._sock
    if (new_sock and (not use_ssl)):
        self.sock = self._sock
    if ((old_conf is not None) and (use_ssl is True) and (old_use_ssl is True)):
        if has_changed('cert_file'):
            self.sock.certfile = self.conf.cert_file
        if has_changed('key_file'):
            self.sock.keyfile = self.conf.key_file
    if (new_sock or ((old_conf is not None) and has_changed('tcp_keepidle'))):
        if hasattr(socket, 'TCP_KEEPIDLE'):
            self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, self.conf.tcp_keepidle)
    if ((old_conf is not None) and has_changed('backlog')):
        self.sock.listen(self.conf.backlog)
