import socket


def create_socket(port, host=None, connect=True):
    """Creates and returns a socket

    Args:
        port (int): port number to use
        connect (bool, optional): whether socket should be connected (default=True)
        host (str, optional): hostname to connect to (default="localhost")

    Returns:
        socket: socket created

    """
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    if host is None:
        host = "localhost"
    if connect:
        s.connect((host, port))
    return s