private void Send(Component componentID, int commandID, int errorCode, MessageType messageType, int messageID, List<Tdf> data)
{
var payload = new TdfEncoder(data).Encode();
var stream = new MemoryStream();
// encode payload size
stream.WriteByte((byte)((payload.Length & 0xFFFF) >> 8));
stream.WriteByte((byte)((byte)payload.Length & 0xFF));
// encode header
stream.WriteByte((byte)(((ushort)componentID >> 8) & 0xFF));
stream.WriteByte((byte)((ushort)componentID & 0xFF));
stream.WriteByte((byte)(((ushort)commandID >> 8) & 0xFF));
stream.WriteByte((byte)((ushort)commandID & 0xFF));
stream.WriteByte((byte)(((byte)errorCode >> 8) & 0xFF));
stream.WriteByte((byte)((byte)errorCode & 0xFF));
stream.WriteByte((byte)((byte)messageType * 16));
stream.WriteByte((byte)(((byte)messageID >> 16) & 0xF));
stream.WriteByte((byte)((byte)messageID >> 8));
stream.WriteByte((byte)((byte)messageID & 0xFF));
if (payload != null && payload.Length > 0)
{
stream.Write(payload, 0, payload.Length);
}
byte[] buffer = stream.GetBuffer();
int position = (int)stream.Position;
var outData = buffer.Take(position).ToArray();
Stream.Write(outData, 0, outData.Length);
Stream.Flush();
}