public TransportSegment(TransportHeader transportHeader)
 {
     TransportHeader = transportHeader;
 }
        public List <byte[]> PackDown(byte[] data, bool isRequest, bool isMaster)
        {
            List <byte[]> segments = new List <byte[]>();

            bool fir      = true;
            byte sequence = 0;

            TransportHeader  transportHeader  = new TransportHeader();
            TransportSegment transportSegment = new TransportSegment(transportHeader);

            int dataCount = data.Count();
            int dataIndex = 0;

            while (dataCount > 0)
            {
                if (sequence == 64)
                {
                    sequence = 0;
                }
                else
                {
                    sequence++;
                }

                int totalLength = 1;

                byte[] temp = null;

                if (fir)
                {
                    transportHeader.Header[6] = true;       // FIR
                    fir      = false;
                    sequence = 0;
                }
                else
                {
                    transportHeader.Header[6] = false;       // FIR
                }

                if (dataCount <= segmentMaxSize)
                {
                    transportHeader.Header[7] = true;       // FIN

                    temp = new byte[dataCount + 1];

                    for (int i = 1; i <= dataCount; i++)
                    {
                        temp[i] = data[dataIndex++];
                    }

                    totalLength += dataCount;
                }
                else
                {
                    temp = new byte[segmentMaxSize + 1];

                    for (int i = 1; i <= dataCount; i++)
                    {
                        temp[i] = data[dataIndex++];
                    }

                    totalLength += segmentMaxSize;
                }

                dataCount -= segmentMaxSize;
                dataIndex += segmentMaxSize;

                BitArray tempSeq = new BitArray(new byte[] { sequence });

                transportHeader.Header[5] = tempSeq[5];      // Sequence
                transportHeader.Header[4] = tempSeq[4];
                transportHeader.Header[3] = tempSeq[3];
                transportHeader.Header[3] = tempSeq[2];
                transportHeader.Header[2] = tempSeq[1];
                transportHeader.Header[0] = tempSeq[0];

                temp[0] = transportHeader.ToBytes()[0];

                byte[] finalDataLinkSegment = new byte[totalLength];

                for (int i = 0; i < totalLength; i++)
                {
                    finalDataLinkSegment[i] = temp[i];
                }

                segments.Add(finalDataLinkSegment);
            }

            List <byte[]> returnValue = new List <byte[]>();

            DNP3DataLinkHandler = new DataLinkHandler();

            foreach (byte[] segment in segments)
            {
                List <byte[]> tempSegments = new List <byte[]>()
                {
                    DNP3DataLinkHandler.PackDown(segment, isRequest, isMaster, DataLinkFunctionCodes.UNCONFIRMED_USER_DATA)
                };

                foreach (byte[] tempSegment in tempSegments)
                {
                    returnValue.Add(tempSegment);
                }
            }

            return(returnValue);
        }