async def incoming_connection(self, request):
    if ((request.remote in self.banned_peers) and (time.time() < self.banned_peers[request.remote])):
        self.log.warning(f'Peer {request.remote} is banned, refusing connection')
        return None
    ws = web.WebSocketResponse(max_msg_size=((50 * 1024) * 1024))
    (await ws.prepare(request))
    close_event = asyncio.Event()
    cert_bytes = request.transport._ssl_protocol._extra['ssl_object'].getpeercert(True)
    der_cert = x509.load_der_x509_certificate(cert_bytes)
    peer_id = bytes32(der_cert.fingerprint(hashes.SHA256()))
    if (peer_id == self.node_id):
        return ws
    connection: Optional[WSGreenBerryConnection] = None
    try:
        connection = WSGreenBerryConnection(self._local_type, ws, self._port, self.log, False, False, request.remote, self.incoming_messages, self.connection_closed, peer_id, self._inbound_rate_limit_percent, self._outbound_rate_limit_percent, close_event)
        handshake = (await connection.perform_handshake(self._network_id, protocol_version, self._port, self._local_type))
        assert (handshake is True)
        if ((not self.accept_inbound_connections(connection.connection_type)) and (not is_in_network(connection.peer_host, self.exempt_peer_networks))):
            self.log.info(f'Not accepting inbound connection: {connection.get_peer_info()}.Inbound limit reached.')
            (await connection.close())
            close_event.set()
        else:
            (await self.connection_added(connection, self.on_connect))
            if ((self._local_type is NodeType.INTRODUCER) and (connection.connection_type is NodeType.FULL_NODE)):
                self.introducer_peers.add(connection.get_peer_info())
    except ProtocolError as e:
        if (connection is not None):
            (await connection.close(self.invalid_protocol_ban_seconds, WSCloseCode.PROTOCOL_ERROR, e.code))
        if (e.code == Err.INVALID_HANDSHAKE):
            self.log.warning('Invalid handshake with peer. Maybe the peer is running old software.')
            close_event.set()
        elif (e.code == Err.INCOMPATIBLE_NETWORK_ID):
            self.log.warning('Incompatible network ID. Maybe the peer is on another network')
            close_event.set()
        elif (e.code == Err.SELF_CONNECTION):
            close_event.set()
        else:
            error_stack = traceback.format_exc()
            self.log.error(f'Exception {e}, exception Stack: {error_stack}')
            close_event.set()
    except Exception as e:
        if (connection is not None):
            (await connection.close(ws_close_code=WSCloseCode.PROTOCOL_ERROR, error=Err.UNKNOWN))
        error_stack = traceback.format_exc()
        self.log.error(f'Exception {e}, exception Stack: {error_stack}')
        close_event.set()
    (await close_event.wait())
    return ws
async def incoming_connection(self, request):
    if ((request.remote in self.banned_peers) and (time.time() < self.banned_peers[request.remote])):
        self.log.warning(f'Peer {request.remote} is banned, refusing connection')
        return None
    ws = web.WebSocketResponse(max_msg_size=((50 * 1024) * 1024))
    (await ws.prepare(request))
    close_event = asyncio.Event()
    cert_bytes = request.transport._ssl_protocol._extra['ssl_object'].getpeercert(True)
    der_cert = x509.load_der_x509_certificate(cert_bytes)
    peer_id = bytes32(der_cert.fingerprint(hashes.SHA256()))
    if (peer_id == self.node_id):
        return ws
    connection: Optional[WSGreenBerryConnection] = None
    try:
        connection = WSGreenBerryConnection(self._local_type, ws, self._port, self.log, False, False, request.remote, self.incoming_messages, self.connection_closed, peer_id, self._inbound_rate_limit_percent, self._outbound_rate_limit_percent, close_event)
        handshake = (await connection.perform_handshake(self._network_id, protocol_version, self._port, self._local_type))
        assert (handshake is True)
        if ((not self.accept_inbound_connections(connection.connection_type)) and (not is_in_network(connection.peer_host, self.exempt_peer_networks))):
            self.log.info(f'Not accepting inbound connection: {connection.get_peer_info()}.Inbound limit reached.')
            (await connection.close())
            close_event.set()
        else:
            (await self.connection_added(connection, self.on_connect))
            if ((self._local_type is NodeType.INTRODUCER) and (connection.connection_type is NodeType.FULL_NODE)):
                self.introducer_peers.add(connection.get_peer_info())
    except ProtocolError as e:
        if (connection is not None):
            (await connection.close(self.invalid_protocol_ban_seconds, WSCloseCode.PROTOCOL_ERROR, e.code))
        if (e.code == Err.INVALID_HANDSHAKE):
            self.log.warning('Invalid handshake with peer. Maybe the peer is running old software.')
            close_event.set()
        elif (e.code == Err.INCOMPATIBLE_NETWORK_ID):
            self.log.warning('Incompatible network ID. Maybe the peer is on another network')
            close_event.set()
        elif (e.code == Err.SELF_CONNECTION):
            close_event.set()
        else:
            error_stack = traceback.format_exc()
            self.log.error(f'Exception {e}, exception Stack: {error_stack}')
            close_event.set()
    except Exception as e:
        if (connection is not None):
            (await connection.close(ws_close_code=WSCloseCode.PROTOCOL_ERROR, error=Err.UNKNOWN))
        error_stack = traceback.format_exc()
        self.log.error(f'Exception {e}, exception Stack: {error_stack}')
        close_event.set()
    (await close_event.wait())
    return ws