/// <summary>
        /// Initializes the next stage of browsing.
        /// </summary>
        private void NextStage()
        {
            ComHdaClientManager system = (ComHdaClientManager)this.SystemContext.SystemHandle;
            ComHdaClient        client = (ComHdaClient)system.SelectClient((ServerSystemContext)SystemContext, false);

            // determine which stage is next based on the reference types requested.
            for (Stage next = m_stage + 1; next <= Stage.Done; next++)
            {
                if (next == Stage.Browse)
                {
                    if (IsRequired(ReferenceTypeIds.Organizes, false))
                    {
                        m_stage = next;
                        break;
                    }
                }

                else if (next == Stage.Children)
                {
                    if (IsRequired(ReferenceTypeIds.HasProperty, false) || IsRequired(ReferenceTypeIds.HasHistoricalConfiguration, false))
                    {
                        m_stage = next;
                        break;
                    }
                }

                else if (next == Stage.Parents)
                {
                    if (IsRequired(ReferenceTypeIds.Organizes, true) || IsRequired(ReferenceTypeIds.HasHistoricalConfiguration, true))
                    {
                        m_stage = next;
                        break;
                    }
                }

                else if (next == Stage.Done)
                {
                    m_stage = next;
                    break;
                }
            }

            // start enumerating areas.
            if (m_stage == Stage.Browse)
            {
                m_browser = null;

                if (m_sourceTypeDefinitionId == Opc.Ua.ObjectTypeIds.FolderType)
                {
                    m_browser = new ComHdaBrowserClient(client, m_itemId);
                }

                return;
            }

            // start enumerating attributes.
            if (m_stage == Stage.Children)
            {
                m_position = 0;
                m_children = null;

                if (m_sourceTypeDefinitionId == Opc.Ua.ObjectTypeIds.FolderType)
                {
                    return;
                }

                List <BaseInstanceState> children = new List <BaseInstanceState>();

                // check if browsing aggregate functions.
                if (m_sourceBrowseName == Opc.Ua.BrowseNames.AggregateFunctions)
                {
                    if (IsRequired(ReferenceTypeIds.HasComponent, false))
                    {
                        BaseObjectState[] aggregates = client.GetSupportedAggregates(m_namespaceIndex);

                        if (aggregates != null)
                        {
                            children.AddRange(aggregates);
                        }

                        m_children = children.ToArray();
                    }

                    return;
                }

                // must be browsing children of an item.
                HdaItem[] items = client.GetItems(m_itemId);

                if (items[0].Error < 0)
                {
                    return;
                }

                try
                {
                    HdaAttributeValue[] attributes = client.ReadAvailableAttributes(items[0]);

                    if (m_sourceTypeDefinitionId == Opc.Ua.VariableTypeIds.DataItemType)
                    {
                        FindChildrenForHdaItem(client, children, attributes);
                    }

                    if (m_sourceTypeDefinitionId == Opc.Ua.ObjectTypeIds.HistoricalDataConfigurationType)
                    {
                        FindChildrenForHdaItemConfiguration(client, children, attributes);
                    }
                }
                finally
                {
                    client.ReleaseItemHandles(items);
                }

                m_children = children.ToArray();
                return;
            }

            // start enumerating parents.
            if (m_stage == Stage.Parents)
            {
                return;
            }

            // all done.
        }