static void Main(string[] args)
        {
            // Use program arguments if specified, otherwise use constants.
            // Arguments are in the sequence [[[<lanxi_ip>] <output_file>] <samples_to_receive>]
            string lanxi_ip    = LANXI_IP;
            string output_file = OUTPUT_FILE;
            Dictionary <string, dynamic> dict;

            if (args.Length == 0)
            {
                string fileName = @"CanStreaming_testInfo.json";
                if (File.Exists(fileName))
                {
                    string[] testInfo = File.ReadAllLines(fileName);
                    foreach (string line in testInfo)
                    {
                        string[] col = line.Split(',');
                        if (string.Compare(col[0], "ModuleIP") == 0)
                        {
                            lanxi_ip = col[1].Trim();
                        }

                        if (string.Compare(col[0], "TestTime") == 0)
                        {
                            testTime = Convert.ToInt32(col[1]);
                        }

                        if (string.Compare(col[0], "NumberOfLoops") == 0)
                        {
                            numberOfTests = Convert.ToInt32(col[1]);
                        }

                        if (string.Compare(col[0], "StopOnError") == 0)
                        {
                            stopOnError = Convert.ToBoolean(col[1]);
                        }

                        if (string.Compare(col[0], "VerifyData") == 0)
                        {
                            VerifyData = Convert.ToBoolean(col[1]);
                        }

                        if (string.Compare(col[0], "EnableObd2") == 0)
                        {
                            obd2Enabled = Convert.ToBoolean(col[1]);
                        }


                        if (string.Compare(col[0], "TestData") == 0)
                        {
                            for (int i = 0; i < 8; i++)
                            {
                                dataToTest[i] = Convert.ToByte(col[i + 1]);
                            }
                        }
                    }
                }
            }
            else
            {
                if (args.Length >= 1)
                {
                    lanxi_ip = args[0];
                }
                if (args.Length >= 2)
                {
                    output_file = args[1];
                }
                if (args.Length >= 3)
                {
                    samples_to_receive = Convert.ToInt32(args[2]);
                }
            }

            testTime *= 1000;                   // Conver it to millisconds.

            // Initialize boundary objects
            LanXIRESTBoundary rest = new LanXIRESTBoundary(lanxi_ip);



            // Start measurement
            if (SetModuleToIdle(rest) < 0)
            {
                return;
            }

            // Connect to CAN-IB module. Use the IPs from the Json file
//            string canibIPs = File.ReadAllText(@"CanStreaming_CanIbIPs.json");
//            if (rest.PutRequestWithPath("/rest/rec/can/connect", canibIPs) == null)
//                goto EXIT_STREAMING;


            // Open the Recorder application on the LAN-XI module
            if (rest.PutRequestWithPath("/rest/rec/open", null) == null)
            {
                goto EXIT_STREAMING;
            }
            rest.WaitForRecorderState("RecorderOpened");


            for (int loop = 0; loop < numberOfTests; loop++)
            {
                idCounter = 0;

                Console.WriteLine("\r\n\r\n************************** Test No: {0} **************************", loop + 1);



                //************************* Auto baud rate detection *************************
                // Note: Baud rate detection requires that CAN channel is connected to a CAN-bus
                //       with CAN activity.
                if (testBaudRateDetectCommand)
                {
                    string detectBaudrate = File.ReadAllText(@"CanStreaming_detectBaudRate.json");
                    dict = rest.PutRequestWithPath("/rest/rec/can/detectBaudRate", detectBaudrate);
                    if (dict == null)
                    {
                        goto EXIT_STREAMING;
                    }

                    var channels = dict["channels"] as ArrayList;
                    foreach (Dictionary <string, object> channel in channels)
                    {
                        var channelId = channel["channel"];
                        var baudRate  = channel["baudRate"];
                        Console.WriteLine("channel={0} baudRate={1}", channelId, baudRate);
                    }
                    //goto EXIT_STREAMING;
                }
                //****************************************************************************


                // Create Recorder configuration
                if (rest.PutRequestWithPath("/rest/rec/create", null) == null)
                {
                    goto EXIT_STREAMING;
                }
                rest.WaitForRecorderState("RecorderConfiguring");


                // Set a configuration for the input channels. Default configuration can be obtained by sending a GET request to /rest/rec/channels/input/default
                // In this example a JSON file has been prepared with the desired configuration.
                string inputChannelConfiguration = File.ReadAllText(@"CanStreaming_ChannelSetup.json");
                if (inputChannelConfiguration.Length > 0)
                {
                    int j = 0;
                    for (int i = 0; i < 2; i++)
                    {
                        JavaScriptSerializer js = new JavaScriptSerializer();
                        dynamic d       = js.Deserialize <dynamic>(inputChannelConfiguration);
                        bool    enabled = Convert.ToBoolean(d["canChannels"][i]["enabled"]);
                        if (enabled == true)
                        {
                            enabledCanChannels[j++] = i + 1;
                        }
                    }
                    //Console.WriteLine("Number enabled CAN channels = {0}.", enabledCanChannels);
                }


/*
 *              var serializer = new JavaScriptSerializer();
 *              Dictionary<string, dynamic> json = serializer.Deserialize<Dictionary<string, object>>(inputChannelConfiguration);
 *
 */

                if (rest.PutRequestWithPath("/rest/rec/channels/input", inputChannelConfiguration) == null)
                {
                    goto EXIT_STREAMING;
                }
                rest.WaitForRecorderState("RecorderStreaming");



                //******************************** OBD-II ***********************************
                // First delete all messages
                string delMsg = File.ReadAllText(@"CanStreaming_DeleteAllObd2.json");
                if (rest.DeleteRequestWithPath("/rest/rec/can/obd2", delMsg) == null)
                {
                    goto EXIT_STREAMING;
                }


                // We assume that CAN channel 1 is looped back to channel 2
                if (obd2Enabled)
                {
                    string obd2Msg          = File.ReadAllText(@"CanStreaming_AddCanObd2.json");
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    dynamic d       = js.Deserialize <dynamic>(obd2Msg);
                    dynamic dataArr = d["Obd2Messages"][0]["data"];
                    var     len     = dataArr.Length;
                    UInt16  chan    = Convert.ToUInt16(d["Obd2Messages"][0]["channel"]);
                    if (chan == 1)
                    {
                        obdChannel = 102;                   // Channel 1 sending OBD2 message and channel 2 receiving data
                    }
                    else if (chan == 2)
                    {
                        obdChannel = 101;                   // Channel 2 sending OBD2 message and channel 1 receiving data
                    }
                    else
                    {
                        Console.WriteLine("**************** ERROR **********************");
                        Console.WriteLine("ERROR: Invalid OBD channel number in the OBD-steup (channel={0}). Discarding OBD-setup.", chan);
                        obd2Enabled = false;
                    }

                    // Overwrite test data with data from first OBD2-message in the CanStreaming_AddCanObd2.json file
                    if (VerifyData)
                    {
                        for (int i = 0; i < 8; i++)
                        {
                            if (i < len)
                            {
                                dataToTest[i] = Convert.ToByte(dataArr[i]);
                            }
                            else
                            {
                                dataToTest[i] = 0;
                            }
                        }
                    }

                    if (rest.PutRequestWithPath("/rest/rec/can/obd2", obd2Msg) == null)
                    {
                        goto EXIT_STREAMING;
                    }
                }
                //string delMsg = File.ReadAllText(@"CanStreaming_DeleteAllObd2.json");
                //if (rest.DeleteRequestWithPath("/rest/rec/can/obd2", delMsg)==null)
                //  goto EXIT_STREAMING;
                //****************************************************************************


                // Get the TCP port provided by the LAN-XI module for streaming samples
                dict = rest.GetRequestWithPath("/rest/rec/destination/socket");
                if (dict == null)
                {
                    goto EXIT_STREAMING;
                }

                UInt16 port = (UInt16)dict["tcpPort"];
                Console.WriteLine("Streaming TCP port: {0}", port);

                // Start measuring
                if (rest.PostRequestWithPath("/rest/rec/measurements", null) == null)
                {
                    goto EXIT_STREAMING;
                }
                rest.WaitForRecorderState("RecorderRecording");


                //***************************** Send Message ********************************
                if (sendMessage > 0)
                {
                    string sendMessageCommand = File.ReadAllText(@"CanStreaming_SendMessages.json");
                    for (int i = 0; i < sendMessage; i++)
                    {
                        if (rest.PutRequestWithPath("/rest/rec/can/sendMessages", sendMessageCommand) == null)
                        {
                            goto EXIT_STREAMING;
                        }
                        Thread.Sleep(10);
                    }
                }
                //****************************************************************************



                // Streaming should now be running

                // Let connectSocketAndStream() method handle socket connection
                // The socket connection may be established while the Recorder was in the "RecorderStreaming" state
                ConnectSocketAndStream(lanxi_ip, port);
//                Console.WriteLine("");

                // Stop measuring and close socket
                if (rest.PutRequestWithPath("/rest/rec/measurements/stop", null) == null)
                {
                    goto EXIT_STREAMING;
                }
                rest.WaitForRecorderState("RecorderStreaming");
                sock.Shutdown(SocketShutdown.Both);
                sock.Close();


                // Free memory used for streaming
                //for (int i = 0; i < outputSamples.Count(); i++)
                //{
                //    outputSamples[i].Clear();
                //}


                if (obd2Enabled)
                {
                    string delAllObd2Msg = File.ReadAllText(@"CanStreaming_DeleteAllObd2.json");
                    if (rest.DeleteRequestWithPath("/rest/rec/can/obd2", delAllObd2Msg) == null)
                    {
                        goto EXIT_STREAMING;
                    }
                }


                if (rest.PutRequestWithPath("/rest/rec/finish", null) == null)
                {
                    goto EXIT_STREAMING;
                }
                rest.WaitForRecorderState("RecorderOpened");
//                Console.WriteLine("*** Number of: messages={0} errors={1} ***", idCounter, totalErrors);
                if (stopOnError && dataError)
                {
                    break;
                }
                idCounter = 0;
                totalReceivedCanPackages = 0;
                dataError = false;
            }

            // Close Recorder application
            rest.PutRequestWithPath("/rest/rec/close", null);
            rest.WaitForRecorderState("Idle");
            return;

EXIT_STREAMING:
            SetModuleToIdle(rest);
            return;

            //StreamWriter file = new StreamWriter(output_file);
            //for (int i = 0; i < outputSamples[0].Count; i++)
            //{
            //    finle.WriteLine("{0}\t{1}\t{2}\t{3}", outputSamples[0][i], outputSamples[1][i], outputSamples[2][i], /*outputSamples[3][i]*/0);
            //}
            //file.Close();
        }