public bool CreateCarriers(InboundRtpProtocol rtp, RtcpProtocol rtcp)
        {
            UDPCarrier pCarrier1 = null;
            UDPCarrier pCarrier2 = null;

            for (var i = 0; i < 10; i++)
            {
                if (pCarrier1 != null)
                {
                    pCarrier1.Dispose();
                    pCarrier1 = null;
                }
                if (pCarrier2 != null)
                {
                    pCarrier2.Dispose();
                    pCarrier2 = null;
                }

                pCarrier1 = UDPCarrier.Create("0.0.0.0", 0);
                if (pCarrier1 == null)
                {
                    WARN("Unable to create UDP carrier for RTP");
                    continue;
                }

                pCarrier2 = (pCarrier1.NearPort % 2) == 0 ? UDPCarrier.Create("0.0.0.0", pCarrier1.NearPort + 1) : UDPCarrier.Create("0.0.0.0", pCarrier1.NearPort - 1);

                if (pCarrier2 == null)
                {
                    WARN("Unable to create UDP carrier for RTCP");
                    continue;
                }

                if (pCarrier1.NearPort > pCarrier2.NearPort)
                {
                    WARN("Switch carriers");
                    UDPCarrier pTemp = pCarrier1;
                    pCarrier1 = pCarrier2;
                    pCarrier2 = pTemp;
                }
                pCarrier1.Protocol         = rtp.FarEndpoint;
                rtp.FarEndpoint.IOHandler  = pCarrier1;
                pCarrier2.Protocol         = rtcp.FarEndpoint;
                rtcp.FarEndpoint.IOHandler = pCarrier2;
                return(pCarrier1.StartAccept() | pCarrier2.StartAccept());
            }

            if (pCarrier1 != null)
            {
                pCarrier1.Dispose();
                pCarrier1 = null;
            }
            if (pCarrier2 != null)
            {
                pCarrier2.Dispose();
                pCarrier2 = null;
            }

            return(false);
        }
        public bool InitializePorts(out Socket socket, out int port, ref NATTraversalProtocol ppNATData, out Socket rtcp, out int rtcpPort, ref NATTraversalProtocol ppNATRTCP)
        {
            UDPCarrier carrier1 = null;
            UDPCarrier carrier2 = null;

            for (int i = 0; i < 10; i++)
            {
                if (carrier1 != null)
                {
                    carrier1.Dispose();
                    carrier1 = null;
                }
                if (carrier2 != null)
                {
                    carrier2.Dispose();
                    carrier2 = null;
                }

                carrier1 = UDPCarrier.Create("0.0.0.0", 0);
                if (carrier1 == null)
                {
                    WARN("Unable to create UDP carrier for RTP");
                    continue;
                }

                carrier2 = (carrier1.NearPort % 2) == 0 ? UDPCarrier.Create("0.0.0.0", carrier1.NearPort + 1) : UDPCarrier.Create("0.0.0.0", carrier1.NearPort - 1);

                if (carrier2 == null)
                {
                    WARN("Unable to create UDP carrier for RTCP");
                    continue;
                }

                if (carrier1.NearPort > carrier2.NearPort)
                {
                    WARN("Switch carriers");
                    UDPCarrier pTemp = carrier1;
                    carrier1 = carrier2;
                    carrier2 = pTemp;
                }

                Variant dummy = Variant.Get();
                socket    = carrier1.Socket;
                port      = carrier1.NearPort;
                ppNATData = (NATTraversalProtocol)ProtocolFactoryManager.CreateProtocolChain(Defines.CONF_PROTOCOL_RTP_NAT_TRAVERSAL, dummy);
                if (ppNATData == null)
                {
                    rtcp     = null;
                    rtcpPort = 0;
                    FATAL("Unable to create the protocol chain {0}", Defines.CONF_PROTOCOL_RTP_NAT_TRAVERSAL);
                    return(false);
                }
                carrier1.Protocol = ppNATData.FarEndpoint;
                ppNATData.FarEndpoint.IOHandler = carrier1;
                rtcp      = carrier2.Socket;
                rtcpPort  = carrier2.NearPort;
                ppNATRTCP = (NATTraversalProtocol)ProtocolFactoryManager.CreateProtocolChain(Defines.CONF_PROTOCOL_RTP_NAT_TRAVERSAL, dummy);
                if (ppNATRTCP == null)
                {
                    FATAL("Unable to create the protocol chain {0}", Defines.CONF_PROTOCOL_RTP_NAT_TRAVERSAL);
                    ppNATData.EnqueueForDelete();
                    return(false);
                }
                carrier2.Protocol = ppNATRTCP.FarEndpoint;
                ppNATRTCP.FarEndpoint.IOHandler = carrier2;
                return(true);
            }
            if (carrier1 != null)
            {
                carrier1.Dispose();
                carrier1 = null;
            }
            if (carrier2 != null)
            {
                carrier2.Dispose();
                carrier2 = null;
            }
            socket   = null;
            port     = 0;
            rtcp     = null;
            rtcpPort = 0;
            return(false);
        }