import socket
import struct


def read_message_tcp(sock: socket.socket) -> bytes:
    """Read TCP DNS message."""
    ret = b""
    try:
        message_len = sock.recv(2)
        ret = ret + message_len
        expected_len = 2 + struct.unpack("!H", message_len)[0]
        while True:
            portion_len = expected_len - len(ret)     #How many bytes left
            if portion_len == 0:
                break
            portion_len = min(1024, portion_len)

            data = sock.recv(portion_len)
            ret = ret + data
    except Exception:
        pass
    return ret