/// <summary>
        /// This will be called when an error occurred during the call.
        /// </summary>
        private void Call_CallErrorOccured(object sender, VoIPEventArgs <CallError> e)
        {
            IPhoneCall call = sender as IPhoneCall;

            if (sender == null)
            {
                return;
            }

            MediaHandlers.DetachAudio();
            MediaHandlers.DetachVideo();
            DisposeCall(call);

            OnPhoneCallStateChanged(call);
        }
        /// <summary>
        /// This will be called when the state of a call has changed.
        /// </summary>
        private void Call_CallStateChanged(object sender, VoIPEventArgs <CallState> e)
        {
            IPhoneCall call = sender as IPhoneCall;

            if (call == null)
            {
                return;
            }

            CallState state = e.Item;

            OnPhoneCallStateChanged(call);
            CheckStopRingback();
            CheckStopRingtone();

            // start ringtones
            if (state.IsRinging())
            {
                if (call.IsIncoming)
                {
                    MediaHandlers.StartRingtone();
                }
                else
                {
                    MediaHandlers.StartRingback();
                }

                return;
            }

            // call has been answered
            if (state == CallState.Answered)
            {
                return;
            }

            // attach media to the selected call when the remote party sends media data
            if (state.IsRemoteMediaCommunication())
            {
                if (SelectedCall.Equals(call))
                {
                    MediaHandlers.AttachAudio(call);
                    MediaHandlers.AttachAudio(call);
                }
                return;
            }

            // detach media from the selected call in hold state or when the call has ended
            if (state == CallState.LocalHeld || state == CallState.InactiveHeld || state.IsCallEnded())
            {
                if (SelectedCall.Equals(call))
                {
                    MediaHandlers.DetachAudio();
                    MediaHandlers.DetachVideo();
                }
            }

            // call has ended, clean up
            if (state.IsCallEnded())
            {
                DisposeCall(call);

                CallHistory.Add(call);
                PhoneCalls.Remove(call);
                return;
            }
        }