private void MainActivity_OnPhoneHangout(object sender, TwilioEventArgs e)
 {
     _connection.Disconnect();
 }
        partial void btnCall(NSObject sender)
        {
            device.StoppedListeningForIncomingConnections += delegate {
                Console.WriteLine("StoppedListeningForIncomingConnection");
                updateStateLabels();
            };

            device.StartedListeningForIncomingConnections += delegate
            {
                Console.WriteLine("StartedListeningForIncomingConnections");
                updateStateLabels();
            };

            device.ReceivedIncomingConnection += (s, a) =>
            {
                Console.WriteLine("ReceivedIncomingConnection");

                if (connection != null && connection.State == TCConnectionState.Connected)
                {
                    connection.Disconnect();
                }

                connection = a.Connection;

                updateStateLabels();
            };

            device.ReceivedPresenceUpdate += delegate { Console.WriteLine("ReceivedPresenceUpdate"); };

            NSDictionary param = NSDictionary.FromObjectsAndKeys(
                new object[] { "+14159929754", "+13144586142" },
                new object[] { "Source", "Target" }
                );

            // HACK:
            // In order to setup events you need to first initialize connection so we have a reference to it, also the delegate param should be null
            // this is because when you set up an event we internally will create a Objc delegate that will be assigned to the delegate property
            // and if we see that delegate property is not null then we will not be able to set you up

            connection = device.Connect(param, null);

            connection.Failed += delegate
            {
                Console.WriteLine("FailedWithError");
                updateStateLabels();
            };
            connection.StartedConnecting += delegate
            {
                Console.WriteLine("StartedConnecting");
                updateStateLabels();
            };
            connection.Connected += delegate
            {
                Console.WriteLine("Connected");
                updateStateLabels();
            };
            connection.Disconnected += delegate
            {
                Console.WriteLine("Disconnected");
                updateStateLabels();
                connection = null;
            };
        }
        public async override void FinishedLaunching(UIApplication application)
        {
            this.Window = new UIWindow(UIScreen.MainScreen.Bounds);

            var client = new HttpClient();
            var token  = await client.GetStringAsync("** Your AuthToken URL goes here **");

            device = new TCDevice(token, null);

            var dialogController = new DialogViewController(new RootElement("Twilio Client Test")
            {
                new Section("Call Options")
                {
                    (numberOrClient = new EntryElement("Target", "Phone # or client name", "")),
                    (callButton = new StyledStatusStringElement("Call", delegate {
                        if (connection == null || connection.State == TCConnectionState.Disconnected)
                        {
                            var param = new TCConnectionParameters {
                                Source = "** Your Twilio Phone Number goes here **",
                                Target = numberOrClient.Value
                            };

                            connection = device.Connect(param, null);

                            SetupConnectionEvents();
                        }
                        else
                        {
                            connection.Disconnect();
                        }
                    })),
                },
                new Section("Status")
                {
                    (deviceState = new StatusStringElement("Device", device.State.ToString())),
                    (connectionState = new StatusStringElement("Connection", "Uninitialized"))
                },

                new Section("Sounds")
                {
                    (disconnectSoundEnabled = new BooleanElement("Disconnect Sound", device.DisconnectSoundEnabled)),
                    (incomingCallSoundEnabled = new BooleanElement("Incoming Sound", device.IncomingSoundEnabled)),
                    (outgoingCallSoundEnabled = new BooleanElement("Outgoing Sound", device.OutgoingSoundEnabled))
                },

                new Section("Options")
                {
                    (muted = new BooleanElement("Muted", false)),
                    (listening = new BooleanElement("Device Listening", true))
                }
            });

            callButton.Alignment = UITextAlignment.Center;
            callButton.TextColor = UIColor.FromRGB(0x00, 0x7A, 0xFF);

            var navigationController = new UINavigationController(dialogController);

            //navigationController.NavigationBar.BarTintColor = UIColor.Red;
            //navigationController.NavigationBar.TintColor = UIColor.White;

            Window.RootViewController = navigationController;

            Window.MakeKeyAndVisible();

            SetupSoundOptionEvents();
            SetupOptions();
            SetupDeviceEvents();
            device.Listen();
        }