def calculate_tcp_checksum(message):
    """ Calculate the TCP checksum
    @param message: payload + TCP headers + pseudoheader
    @return: 16-bit TCP checksum value
    """
    cs = 0

    for i in range(0, len(message), 2):
        w = (ord(message[i])<<8 + ord(message[i+1]))
        cs = cs + w

    cs = (cs>>16) + (cs & 0xffff)
    cs = ~cs & 0xffff

    return cs