@Override
	public Packet write(DataOutputStream out, PassthroughConnection ptc, KillableThread thread, boolean serverToClient) {
		int oldCounter = 0;
		if(ptc != null) {
			oldCounter = ptc.packetCounter;
		}
		
		if(!setupFields()) {
			ptc.printLogMessage("Error creating field data storage for packet: " + packetId);
			return null;
		}

		int length = fields.length;
		for(int cnt=0;cnt<length;cnt++) {
			if(fields[cnt].write(out, ptc, null, serverToClient)==null) {
				return null;
			}
		}
		if(packetId != null && ptc != null) {
			int newCounter = ptc.packetCounter;
			ptc.packetCounters[packetId & 0xFF] += newCounter - oldCounter;
		}
		
		try {
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		
		return this;
	}