import socket


def find_unused_port(base=1025, maxtries=10000):
    """Helper function. Finds an unused server port"""
    if base > 65535:
        base = 1025
    for _ in range(maxtries):
        serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            serversocket.bind(("localhost", base))
            serversocket.close()
            return base
        except:
            base += 1
            continue
    raise RuntimeError("unable to find free socket port")