import socket
import time


def create_socket(url: str, port: int, client: bool = True):
    """Helper function to create a new socket connection
        Arguments:
            - url (str): url to connect/listen to
            - port (int): port to connect/listen to
            - client (bool): If False return a listener
        Returns:
            - socket.socket
    """
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    if client:
        for _ in range(50): #10 sek retry
            try:
                sock.connect((url, port))
                return sock
            except:
                time.sleep(0.2)
        raise ConnectionError("Couldn't connect to socket...")
    sock.bind((url, port))
    sock.listen()
    return sock