/// <summary>
 ///   Creates a I/O event argument object which contains an OSC bundle.
 /// </summary>
 public OscIoDeviceEventArgs(OscBundle bundle,
                             OscIoDeviceChannel deviceChannel,
                             Exception exception = null)
 {
     Bundle = bundle;
     DeviceChannel = deviceChannel;
     Exception = exception;
 }
        /// <summary>
        ///   Sends a OSC bundle to the given UDP address.
        /// </summary>
        public void Send(OscBundle bundle,
                         OscIoDeviceChannel deviceChannel)
        {
            if (ReferenceEquals(bundle, null))
                throw new ArgumentNullException("bundle");

            byte[] packet = bundle.ToOscPacketArray();
            IPEndPoint ipEndPoint = deviceChannel != null ? deviceChannel.IPEndPoint : _defaultRemoteEndPoint;
            OscIoDeviceEventArgs eventArgs = new OscIoDeviceEventArgs(bundle,
                                                                      new OscIoDeviceChannel(
                                                                          OscIoDeviceChannelType.Udp,
                                                                          ipEndPoint));
            //send bundle
            _udp.BeginSend(packet, packet.Length, ipEndPoint, OnSend, eventArgs);
        }
        /// <summary>
        ///   Parses a bundle packet.
        /// </summary>
        private static OscBundle ParseBundle(byte[] packet)
        {
            int packetIndex = 8; // skip #bundle

            OscBundle bundle = new OscBundle
                                   {
                                       Timetag = ParseTimetag(packet, ref packetIndex)
                                   };

            do
            {
                // get element size
                int size = ParseBundleElementSize(packet, ref packetIndex);

                // get element bytes
                byte[] element = ParseBundleElement(packet, ref packetIndex, size);

                if (IsBundle(element))
                {
                    bundle.Elements.Add(ParseBundle(element));
                }
                else
                {
                    bundle.Elements.Add(ParseMessage(element));
                }
            } while (packetIndex < packet.Length);

            return bundle;
        }