/// <summary>
        /// Gets the patient record.
        /// </summary>
        /// <param name="hdid">The patient id.</param>
        /// <returns>The patient model.</returns>
        public async System.Threading.Tasks.Task <Patient> GetPatient(string hdid)
        {
            // Create request
            HCIM_IN_GetDemographics request = this.CreateRequest(hdid);

            // Perform the request
            HCIM_IN_GetDemographicsResponse1 reply = await this.getDemographicsClient.HCIM_IN_GetDemographicsAsync(request).ConfigureAwait(true);

            // Verify that the reply contains a result
            string responseCode = reply.HCIM_IN_GetDemographicsResponse.controlActProcess.queryAck.queryResponseCode.code;

            if (responseCode.Contains("BCHCIM.GD.0.0013", System.StringComparison.InvariantCulture))
            {
                HCIM_IN_GetDemographicsResponseIdentifiedPerson retrievedPerson = reply.HCIM_IN_GetDemographicsResponse.controlActProcess.subject[0].target;

                // Extract the subject names
                List <string> givenNameList = new List <string>();
                List <string> lastNameList  = new List <string>();
                for (int i = 0; i < retrievedPerson.identifiedPerson.name[0].Items.Length; i++)
                {
                    ENXP name = retrievedPerson.identifiedPerson.name[0].Items[i];

                    if (name.GetType() == typeof(engiven))
                    {
                        givenNameList.Add(name.Text[0]);
                    }
                    else if (name.GetType() == typeof(enfamily))
                    {
                        lastNameList.Add(name.Text[0]);
                    }
                }

                string delimiter  = " ";
                string givenNames = givenNameList.Aggregate((i, j) => i + delimiter + j);
                string lastNames  = lastNameList.Aggregate((i, j) => i + delimiter + j);
                string phn        = ((II)retrievedPerson.identifiedPerson.id.GetValue(0)).extension;
                // For now, add the return message to the reply
                return(new Patient(hdid, phn, givenNames, lastNames));
            }
            else
            {
                this.logger.LogInformation("Client Registry did not return a person. Returned message code: " + responseCode);
                return(new Patient());
            }
        }
        private static HCIM_IN_GetDemographicsRequest CreateRequest(OIDType oIDType, string identifierValue)
        {
            using (Source.StartActivity("CreatePatientSOAPRequest"))
            {
                HCIM_IN_GetDemographics request = new HCIM_IN_GetDemographics();
                request.id = new II()
                {
                    root = "2.16.840.1.113883.3.51.1.1.1", extension = DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString(System.Globalization.CultureInfo.InvariantCulture)
                };
                request.creationTime = new TS()
                {
                    value = System.DateTime.Now.ToString("yyyyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture)
                };
                request.versionCode = new CS()
                {
                    code = "V3PR1"
                };
                request.interactionId = new II()
                {
                    root = "2.16.840.1.113883.3.51.1.1.2", extension = "HCIM_IN_GetDemographics"
                };
                request.processingCode = new CS()
                {
                    code = "P"
                };
                request.processingModeCode = new CS()
                {
                    code = "T"
                };
                request.acceptAckCode = new CS()
                {
                    code = "NE"
                };

                request.receiver = new MCCI_MT000100Receiver()
                {
                    typeCode = "RCV"
                };
                request.receiver.device = new MCCI_MT000100Device()
                {
                    determinerCode = "INSTANCE", classCode = "DEV"
                };
                request.receiver.device.id = new II()
                {
                    root = "2.16.840.1.113883.3.51.1.1.4", extension = "192.168.0.1"
                };
                request.receiver.device.asAgent = new MCCI_MT000100Agent()
                {
                    classCode = "AGNT"
                };
                request.receiver.device.asAgent.representedOrganization = new MCCI_MT000100Organization()
                {
                    determinerCode = "INSTANCE", classCode = "ORG"
                };
                request.receiver.device.asAgent.representedOrganization = new MCCI_MT000100Organization()
                {
                    determinerCode = "INSTANCE", classCode = "ORG"
                };
                request.receiver.device.asAgent.representedOrganization.id = new II()
                {
                    root = "2.16.840.1.113883.3.51.1.1.3", extension = "HCIM"
                };

                request.sender = new MCCI_MT000100Sender()
                {
                    typeCode = "SND"
                };
                request.sender.device = new MCCI_MT000100Device()
                {
                    determinerCode = "INSTANCE", classCode = "DEV"
                };
                request.sender.device.id = new II()
                {
                    root = "2.16.840.1.113883.3.51.1.1.5", extension = "MOH_CRS"
                };
                request.sender.device.asAgent = new MCCI_MT000100Agent()
                {
                    classCode = "AGNT"
                };
                request.sender.device.asAgent.representedOrganization = new MCCI_MT000100Organization()
                {
                    determinerCode = "INSTANCE", classCode = "ORG"
                };
                request.sender.device.asAgent.representedOrganization = new MCCI_MT000100Organization()
                {
                    determinerCode = "INSTANCE", classCode = "ORG"
                };
                request.sender.device.asAgent.representedOrganization.id = new II()
                {
                    root = "2.16.840.1.113883.3.51.1.1.3", extension = "HGWAY"
                };

                request.controlActProcess = new HCIM_IN_GetDemographicsQUQI_MT020001ControlActProcess()
                {
                    classCode = "ACCM", moodCode = "EVN"
                };
                request.controlActProcess.effectiveTime = new IVL_TS()
                {
                    value = System.DateTime.Now.ToString("yyyyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture)
                };
                request.controlActProcess.dataEnterer = new QUQI_MT020001DataEnterer()
                {
                    typeCode = "CST", time = null, typeId = null
                };
                request.controlActProcess.dataEnterer.assignedPerson = new COCT_MT090100AssignedPerson()
                {
                    classCode = "ENT"
                };

                // TODO: We should likely send the actual username instead of HLTHGTWAY
                request.controlActProcess.dataEnterer.assignedPerson.id = new II()
                {
                    root = "2.16.840.1.113883.3.51.1.1.7", extension = "HLTHGTWAY"
                };

                request.controlActProcess.queryByParameter = new HCIM_IN_GetDemographicsQUQI_MT020001QueryByParameter();
                request.controlActProcess.queryByParameter.queryByParameterPayload                = new HCIM_IN_GetDemographicsQueryByParameterPayload();
                request.controlActProcess.queryByParameter.queryByParameterPayload.personid       = new HCIM_IN_GetDemographicsPersonid();
                request.controlActProcess.queryByParameter.queryByParameterPayload.personid.value = new II()
                {
                    root = oIDType.ToString(), extension = identifierValue, assigningAuthorityName = "LCTZ_IAS"
                };

                return(new HCIM_IN_GetDemographicsRequest(request));
            }
        }