def ber_from_socket(s):
    """
    Reads a ber length from a socket.
    Reads first byte to read the ber length first, then read the actual data.
    return: (length, bytes_read, data) - a tuple of values
    """
    if isinstance(s, network.SocketClient):
        recv = s.receive
    else:
        recv = s.recv
    data = recv(1)

    length = data[0]
    bytes_read = 1
    if length > 127:
        bytes_read += length & 127 # Strip off the high bit
        data += recv(bytes_read - 1)
        length = 0
        for i in range(1, bytes_read):
            length += data[i] << (8 * (bytes_read - i - 1))
    return length, bytes_read, data