class TCP extends BasePacket {

    public byte[] serialize() {
        int length;
        if (dataOffset == 0)
            dataOffset = 5;  // default header length
        length = dataOffset << 2;
        byte[] payloadData = null;
        if (payload != null) {
            payload.setParent(this);
            payloadData = payload.serialize();
            length += payloadData.length;
        }

        byte[] data = new byte[length];
        ByteBuffer bb = ByteBuffer.wrap(data);

        bb.putShort((short)this.sourcePort.getPort()); //TCP ports are defined to be 16 bits
        bb.putShort((short)this.destinationPort.getPort());
        bb.putInt(this.sequence);
        bb.putInt(this.acknowledge);
        bb.putShort((short) (this.flags | (dataOffset << 12)));
        bb.putShort(this.windowSize);
        bb.putShort(this.checksum);
        bb.putShort(this.urgentPointer);
        if (dataOffset > 5) {
            int padding;
            bb.put(options);
            padding = (dataOffset << 2) - 20 - options.length;
            for (int i = 0; i < padding; i++)
                bb.put((byte) 0);
        }
        if (payloadData != null)
            bb.put(payloadData);

        if (this.parent != null && this.parent instanceof IPv4)
            ((IPv4)this.parent).setProtocol(IpProtocol.TCP);

        // compute checksum if needed
		if (this.checksum == 0) {

			if (this.parent != null && this.parent instanceof IPv4) {
			    //Checksum calculation based on the JSocket Wrench code
				// https://github.com/ehrmann/jswrench
				//The original code can be found at 
				//https://github.com/ehrmann/jswrench/blob/master/src/com/act365/net/SocketUtils.java
				IPv4 ipv4 = (IPv4) this.parent;

				int bufferlength = length + 12;
				boolean odd = length % 2 == 1;
				byte[] source = ipv4.getSourceAddress().getBytes();
				byte[] destination = ipv4.getDestinationAddress().getBytes();

				if (odd) {
					++bufferlength;
				}

				byte[] buffer = new byte[bufferlength];

				buffer[0] = source[0];
				buffer[1] = source[1];
				buffer[2] = source[2];
				buffer[3] = source[3];

				buffer[4] = destination[0];
				buffer[5] = destination[1];
				buffer[6] = destination[2];
				buffer[7] = destination[3];

				buffer[8] = (byte) 0;
				buffer[9] = (byte) ipv4.getProtocol().getIpProtocolNumber();

				shortToBytes((short) length, buffer, 10);

				int i = 11;

				while (++i < length + 12) {
					buffer[i] = data[i + 0 - 12];
				}

				if (odd) {
					buffer[i] = (byte) 0;
				}

				this.checksum = checksum(buffer, buffer.length, 0);
			} else {
				bb.rewind();
				int accumulation = 0;
				for (int i = 0; i < length / 2; ++i) {
					accumulation += 0xffff & bb.getShort();
				}
				// pad to an even number of shorts
				if (length % 2 > 0) {
					accumulation += (bb.get() & 0xff) << 8;
				}

				accumulation = ((accumulation >> 16) & 0xffff)
						+ (accumulation & 0xffff);
				this.checksum = (short) (~accumulation & 0xffff);
			}

			bb.putShort(16, this.checksum);
		}
        
        return data;
    }

}