// Restore message properties before passing the message to application
        private void PrepareLargeMessage(LargeMessageProperties largeMessageProperties)
        {
            if (this.stream != null)
            {
                this.stream.Position = 0;
                if (this.useBody)
                {
                    byte[] fullBytes = new byte[this.stream.Length];
                    this.stream.Read(fullBytes, 0, fullBytes.Length);
                    if (this.message.Body is Stream)
                    {
                        this.message.BodyStream.Dispose();
                        ((Stream)this.message.Body).Dispose();

                        this.message.BodyStream = new MemoryStream(fullBytes, 0, fullBytes.Length);
                        this.message.Body = this.message.BodyStream;
                    }
                    else
                    {
                        // The body has to be set to null so that the formatter read method (deserialize) is invoked
                        this.message.Body = null;

                        try
                        {
                            this.message.BodyStream.Dispose();
                        }
                        catch
                        {
                            // Don't do anything
                        }

                        this.message.BodyStream = new MemoryStream(fullBytes, 0, fullBytes.Length);
                    }
                }
                else
                {
                    try
                    {
                        this.message.BodyStream.Dispose();
                    }
                    catch
                    {
                        // Don't do anything
                    }

                    this.message.BodyStream = this.stream;
                }
            }
            else
            {
                this.message.Body = null;
                this.message.BodyStream = new MemoryStream();
            }

            // Setting the fields to that passed from the application
            this.message.AppSpecific = largeMessageProperties.AppSpecific;
            this.message.CorrelationId = largeMessageProperties.CorrelationId;

            // Setting the message Label to the application message label from the sender side
            string messageLabel = this.message.Label;
            try
            {
                this.message.Label = messageLabel.Substring(0, messageLabel.LastIndexOf(".part.", StringComparison.OrdinalIgnoreCase));
            }
            catch (ArgumentOutOfRangeException)
            {
                this.message.Label = messageLabel;
            }
        }
        // Return the large message properties embedded in the control (header/trailer) fragments
        private LargeMessageProperties GetLargeMessageHeader(Message headerMessage)
        {
            byte[] b = null;
            if (headerMessage.Body != null)
            {
                if (headerMessage.Body is Stream)
                {
                    b = Utilities.GetBytesFromStream((Stream)headerMessage.Body);
                }
                else
                {
                    b = (byte[])headerMessage.Body;
                }

                this.useBody = true;
            }
            else if (headerMessage.BodyStream != null)
            {
                Stream s = headerMessage.BodyStream;
                b = new byte[s.Length];
                s.Read(b, 0, b.Length);
                s.Position = 0;

                this.useBody = false;
            }

            LargeMessageProperties largeMessageProperties = new LargeMessageProperties();
            byte[] fragmentCountBytes = new byte[4];
            byte[] appSpecificBytes = new byte[4];
            byte[] correlationIdLengthBytes = new byte[4];

            System.Buffer.BlockCopy(b, 0, fragmentCountBytes, 0, 4);
            largeMessageProperties.FragmentCount = BitConverter.ToInt32(fragmentCountBytes, 0);

            System.Buffer.BlockCopy(b, 4, appSpecificBytes, 0, 4);
            largeMessageProperties.AppSpecific = BitConverter.ToInt32(appSpecificBytes, 0);

            System.Buffer.BlockCopy(b, 8, correlationIdLengthBytes, 0, 4);
            int correlationIdLength = BitConverter.ToInt32(correlationIdLengthBytes, 0);

            if (correlationIdLength != 0)
            {
                byte[] correlationIdBytes = new byte[correlationIdLength];
                System.Buffer.BlockCopy(b, 12, correlationIdBytes, 0, correlationIdLength);
                largeMessageProperties.CorrelationId = Encoding.Unicode.GetString(correlationIdBytes);
            }
            else
            {
                largeMessageProperties.CorrelationId = Utilities.GetEmptyCorrelationIdStr();
            }

            return largeMessageProperties;
        }