static void OnPacketReceived(IAsyncResult result)
        {
            //Get data and retrieve TelemetryFrame
            byte[]     message = socket.EndReceive(result, ref endPoint);
            ByteBuffer b       = new ByteBuffer(message);

            KartKraft.Frame frame = KartKraft.Frame.GetRootAsFrame(b);

            Console.WriteLine("Received frame of length " + message.Length);

            //Handle Motion
            if (frame.Motion.HasValue)
            {
                Console.WriteLine("  motion data:");
                Console.WriteLine("    angles {0} {1} {2}", frame.Motion.Value.Pitch, frame.Motion.Value.Roll, frame.Motion.Value.Yaw);
                Console.WriteLine("    angularVel {0} {1} {2}", frame.Motion.Value.AngularVelocityX, frame.Motion.Value.AngularVelocityY, frame.Motion.Value.AngularVelocityZ);
                Console.WriteLine("    vel {0} {1} {2}", frame.Motion.Value.VelocityX, frame.Motion.Value.VelocityY, frame.Motion.Value.VelocityZ);
                for (int i = 0; i < frame.Motion.Value.WheelsLength; i++)
                {
                    if (frame.Motion.Value.Wheels(i).HasValue)
                    {
                        KartKraft.Wheel wheel = frame.Motion.Value.Wheels(i).Value;
                        Console.WriteLine("    wheel {0} surface {1} slipAngle {2} ", i, wheel.Surface, wheel.SlipAngle);
                    }
                }
            }

            //Handle Dashboard
            if (frame.Dash.HasValue)
            {
                Console.WriteLine("  dash data:");
                Console.WriteLine("    rpm {0}", frame.Dash.Value.Rpm);
                Console.WriteLine("    speed {0}", frame.Dash.Value.Speed);
                Console.WriteLine("    steer {0}", frame.Dash.Value.Steer);
                Console.WriteLine("    throttle {0}", frame.Dash.Value.Throttle);
                Console.WriteLine("    brake {0}", frame.Dash.Value.Brake);
                Console.WriteLine("    gear {0}", frame.Dash.Value.Gear);
                Console.WriteLine("    pos {0}", frame.Dash.Value.Pos);
                Console.WriteLine("    best lap {0}", frame.Dash.Value.BestLap);
                Console.WriteLine("    current lap {0}", frame.Dash.Value.CurrentLap);
                Console.WriteLine("    last lap {0}", frame.Dash.Value.LastLap);
                Console.WriteLine("    lap count {0}", frame.Dash.Value.LapCount);
            }

            //Handle Session
            if (frame.Session.HasValue)
            {
                Console.WriteLine("    session:" + " ");
            }

            //Schedule the next receive operation
            socket.BeginReceive(callback, socket);
        }
        private KartKraftData parseResponse(KartKraft.Frame resp)
        {
            KartKraftData outData = new KartKraftData();

            try
            {
                outData.AccelerationX = resp.Motion.Value.AccelerationX;
                outData.AccelerationY = resp.Motion.Value.AccelerationY;
                outData.AccelerationZ = resp.Motion.Value.AccelerationZ;
                outData.Pitch         = resp.Motion.Value.Pitch;
                outData.Roll          = resp.Motion.Value.Roll;
                outData.Yaw           = resp.Motion.Value.Yaw;
                outData.RPM           = resp.Dash.Value.Rpm;
                outData.Speed         = resp.Dash.Value.Speed;
            }catch (Exception e)
            {
                // sometimes the response object can be null, so we catch them here.
                // LogError(e.ToString()); // nothing wo log, only null values
            }
            return(outData);
        }
        /// <summary>
        /// Connect to the TCP Server of No Limits 2
        /// </summary>
        private void Run()
        {
            //TODO
            KartKraftData lastTelemetryData = new KartKraftData();
            Stopwatch     sw = new Stopwatch();

            sw.Start();

            UdpClient socket = new UdpClient();

            socket.ExclusiveAddressUse = false;
            socket.Client.Bind(new IPEndPoint(IPAddress.Any, PORTNUM));

            Log("Listener thread started (IP: " + IP + ":" + PORTNUM.ToString() + ") DCSTelemetryProvider.Thread");

            while (!isStopped)
            {
                try
                {
                    // get data from game,
                    if (socket.Available == 0)
                    {
                        if (sw.ElapsedMilliseconds > 500)
                        {
                            IsRunning   = false;
                            IsConnected = false;
                            //Thread.Sleep(1000);
                        }
                        else
                        {
                            Thread.Sleep(1);
                        }
                        continue;
                    }
                    else
                    {
                        IsConnected = true;
                    }

                    // DCS approach....
                    // Byte[] received = socket.Receive(ref _senderIP);
                    // string resp = Encoding.UTF8.GetString(received);
                    //LogDebug(resp);

                    //KK Approach
                    Byte[]          received = socket.Receive(ref _senderIP);
                    ByteBuffer      b        = new ByteBuffer(received);
                    KartKraft.Frame frame    = KartKraft.Frame.GetRootAsFrame(b);

                    KartKraftData telemetryData = parseResponse(frame);

                    IsRunning = true;

                    //TODO
                    TelemetryEventArgs args = new TelemetryEventArgs(
                        new KartKraftTelemetryInfo(telemetryData, lastTelemetryData));
                    RaiseEvent(OnTelemetryUpdate, args);
                    lastTelemetryData = telemetryData;

                    sw.Restart();
                    //Thread.Sleep(SamplePeriod);
                }
                catch (Exception e)
                {
                    LogError(e.ToString());
                    IsConnected = false;
                    IsRunning   = false;
                    Thread.Sleep(1000);
                }
            }
            IsConnected = false;
            IsRunning   = false;

            try
            {
                socket.Close();
            }
            catch (Exception) { }

            Log("Listener thread stopped, KartKraftTelemetryProvider.Thread");
        }