public abstract bool Send(NxtMessage msg);
 public void QueueMessageForSending(NxtMessage msg)
 // Stick this message in the queue to be sent
     {
     lock (this.nxtMessagesToSend)
         {
         this.nxtMessagesToSend.Add(msg);
         this.MessageToSendSemaphore.Release();
         }
     }
        //--------------------------------------------------------------------------
        // Data transmission
        //--------------------------------------------------------------------------

        public override bool Send(NxtMessage msg)
            {
            bool fResult = false;
            if (this.IsOpen)
                {
                try {
                    lock (this.msgReplyTargets)
                        {
                        this.msgReplyTargets.Add(msg);
                        }

                    byte[] rgbToSend = msg.DataForBluetoothTransmission;
                    int cbSent = 0;

                    unsafe
                        {
                        EventWaitHandle     asyncWriteCompleteEvent = new EventWaitHandle(false, System.Threading.EventResetMode.ManualReset);
                        Overlapped          overlappedWrite         = new Overlapped(-1, -1, asyncWriteCompleteEvent.SafeWaitHandle.DangerousGetHandle(), null);
                        NativeOverlapped*   pNativeOverlappedWrite  = overlappedWrite.Pack(null, rgbToSend);
                        try 
                            {
                            bool fSuccess = WriteFile(this.hSerialPort, rgbToSend, rgbToSend.Length, out cbSent, new IntPtr(pNativeOverlappedWrite));
                            if (!fSuccess)
                                {
                                int err = Marshal.GetLastWin32Error();
                                if (ERROR_IO_PENDING == err)
                                    {
                                    asyncWriteCompleteEvent.WaitOne();
                                    }
                                else
                                    ThrowWin32Error(err);
                                }
                            }
                        finally
                            {
                            System.Threading.Overlapped.Free(pNativeOverlappedWrite);
                            }
                        }

                    msg.NoteSent();
                    fResult = true;
                    }
                catch (Exception)
                    {
                    }
                }
            return fResult;
            }
        //--------------------------------------------------------------------------
        // Data transmission
        //--------------------------------------------------------------------------

        public override bool Send(NxtMessage msg)
            {
            lock (this.msgReplyTargets)
                {
                this.msgReplyTargets.Add(msg);
                }

            byte[] rgbToSend = msg.DataForUSBTransmission;

            int cbWritten = 0;
            bool fSuccess = WinUsb_WritePipe(
                hWinUSB,
                bulkOutPipe,
                rgbToSend,
                rgbToSend.Length,
                ref cbWritten,
                IntPtr.Zero);

            if (fSuccess)
                {
                msg.NoteSent();
                }
            else
                {
                this.Close();
                }

            return fSuccess;
            }        
        //--------------------------------------------------------------------------
        // Data transmission
        //--------------------------------------------------------------------------

        public override bool Send(NxtMessage msg)
            {
            bool fResult = false;
            this.SocketMonitorLock(() =>
                {
                if (this.IsOpen)
                    {
                    try
                        {
                        lock (this.msgReplyTargets)
                            {
                            this.msgReplyTargets.Add(msg);
                            }

                        byte[] rgbToSend = msg.DataForIPTransmission;

                        this.socket.Send(rgbToSend);

                        msg.NoteSent();
                        fResult = true;
                        }
                    catch (Exception e)
                        {
                        // Remove the msg if we have issues
                        //
                        Program.Trace("IPConnection.Send({0}): exception thrown: {1}", this.epSamanthaTCP, e);
                        lock (this.msgReplyTargets)
                            {
                            for (int imsg = 0; imsg < this.msgReplyTargets.Count; imsg++)
                                {
                                if (this.msgReplyTargets[imsg] == msg)
                                    {
                                    this.msgReplyTargets.RemoveAt(imsg);
                                    break;
                                    }
                                }
                            }
                        }
                    }
                });
            return fResult;
            }
 public PayloadAccessor(NxtMessage msg)
 {
     this.msg = msg;
 }