private PcfEnvironment(IPEndPoint cfInstanceAddr, string cfInstanceGuid, int?cfInstanceIndex,
                        IPAddress cfInstanceIp, int?cfInstancePort, IReadOnlyList <PortMapping> cfInstancePorts,
                        string home, string lang, int?port, string pwd, string tmpdir, string user,
                        VcapApplication vcapApplication, IPAddress cfInstanceInternalIp)
 {
     CF_INSTANCE_ADDR  = cfInstanceAddr;
     CF_INSTANCE_GUID  = cfInstanceGuid;
     CF_INSTANCE_INDEX = cfInstanceIndex;
     CF_INSTANCE_IP    = cfInstanceIp;
     CF_INSTANCE_PORT  = cfInstancePort;
     CF_INSTANCE_PORTS = cfInstancePorts;
     HOME                    = home;
     LANG                    = lang;
     PORT                    = port;
     PWD                     = pwd;
     TMPDIR                  = tmpdir;
     USER                    = user;
     VCAP_APPLICATION        = vcapApplication;
     CF_INSTANCE_INTERNAL_IP = cfInstanceInternalIp;
 }
        internal static PcfEnvironment Init()
        {
            IPEndPoint cfInstanceAddr = null;
            IPAddress  cfInstanceIp   = null;
            int?       cfInstancePort = null;

            var instanceIp   = Environment.GetEnvironmentVariable("CF_INSTANCE_IP");
            var instancePort = Environment.GetEnvironmentVariable("CF_INSTANCE_PORT");

            if (!string.IsNullOrEmpty(instancePort) && !string.IsNullOrEmpty(instanceIp))
            {
                cfInstanceIp   = IPAddress.Parse(instanceIp);
                cfInstancePort = int.Parse(instancePort);
                cfInstanceAddr = new IPEndPoint(cfInstanceIp, cfInstancePort.Value);
            }

            IPAddress cfInternalIp       = null;
            var       instanceInternalIp = Environment.GetEnvironmentVariable("CF_INSTANCE_INTERNAL_IP");

            if (!string.IsNullOrEmpty(instanceInternalIp))
            {
                cfInstanceIp = IPAddress.Parse(instanceInternalIp);
            }

            var cfInstanceGuid  = Environment.GetEnvironmentVariable("CF_INSTANCE_GUID");
            var instanceIndex   = Environment.GetEnvironmentVariable("CF_INSTANCE_INDEX");
            int?cfInstanceIndex = null;

            if (!string.IsNullOrEmpty(instanceIndex) && int.TryParse(instanceIndex, out var index))
            {
                cfInstanceIndex = index;
            }

            var cfHome = Environment.GetEnvironmentVariable("HOME");
            var lang   = Environment.GetEnvironmentVariable("LANG");
            var pwd    = Environment.GetEnvironmentVariable("PWD");
            var tmpdir = Environment.GetEnvironmentVariable("TMPDIR");
            var user   = Environment.GetEnvironmentVariable("USER");

            var strPortMappings = Environment.GetEnvironmentVariable("CF_INSTANCE_PORTS");
            IReadOnlyList <PortMapping> mappings = new List <PortMapping>();

            if (!string.IsNullOrEmpty(strPortMappings))
            {
                mappings = PcfSerializer.ParsePcfPorts(strPortMappings);
            }

            VcapApplication vcapApplication     = null;
            var             jsonVcapApplication = Environment.GetEnvironmentVariable("VCAP_APPLICATION");

            if (!string.IsNullOrEmpty(jsonVcapApplication))
            {
                vcapApplication = PcfSerializer.ParseVcapApplication(jsonVcapApplication)
                                  .ToVcapApplication();
            }

            var strPort = Environment.GetEnvironmentVariable("PORT");
            int?port    = null;

            if (!string.IsNullOrEmpty(strPort) && int.TryParse(strPort, out var intPort))
            {
                port = intPort;
            }

            return(new PcfEnvironment(cfInstanceAddr, cfInstanceGuid, cfInstanceIndex, cfInstanceIp, cfInstancePort,
                                      mappings, cfHome, lang, port, pwd, tmpdir, user, vcapApplication, cfInternalIp));
        }