import socket
import time


def read_server_and_port_from_file(server_connection_info):
    """
    Reads the server hostname and port from a file, which must contain
    'hostname port'. Sleeps if the file doesn't exist or formatting was off (so
    you can have clients wait for the server to start running.)
    
    :return: tuple containing hostname and port
    :rtype: Tuple
    """
    while True:
        try:
            with open(server_connection_info, 'r') as f:
                (hostname, port) = f.readline().split(' ')
                port = int(port)
                if hostname == socket.gethostname():
                    hostname = 'localhost'
                return (hostname, port)
        except (ValueError, FileNotFoundError) as e:
            time.sleep(1)
            continue