'Test to bind to a given random port. Try again if the random port turned out to be blocked.'

def test_bind(self):

    for i in range(20):
        random_port = random.randrange(1024, 65535)
        try:
            c = tcp.TCPClient(('127.0.0.1', self.port), source_address=('127.0.0.1', random_port))
            with c.connect():
                assert (c.rfile.readline() == str(('127.0.0.1', random_port)).encode())
                return
        except exceptions.TcpException:
            pass
