'Get or create a connection to a broker using host and port'

def _get_conn(self, host, port, afi):

    host_key = (host, port)
    if (host_key not in self._conns):
        self._conns[host_key] = BrokerConnection(host, port, afi, request_timeout_ms=(self.timeout * 1000), client_id=self.client_id)
    conn = self._conns[host_key]
    conn.connect()
    if conn.connected():
        return conn
    timeout = (time.time() + self.timeout)
    while ((time.time() < timeout) and conn.connecting()):
        if (conn.connect() is ConnectionStates.CONNECTED):
            break
        else:
            time.sleep(0.05)
    else:
        conn.close()
        raise ConnectionError(('%s:%s    (%s)' % (host, port, afi)))
    return conn
