/// <summary>
        /// Gets the parameters.
        /// FO-dicom server initialization does not appear to be thread safe so we use this code to give it ample time to init.
        /// Please access the parameters by calling GetParameters() in an association request.
        /// </summary>
        /// <param name="timeoutSeconds">The time we will wait until the parameters are not null.</param>
        /// <returns>The parameters.</returns>
        private DicomFileStoreParameters GetParameters(uint timeoutSeconds = 5)
        {
            _parameters = UserState as DicomFileStoreParameters;

            if (_parameters == null && timeoutSeconds > 0)
            {
                SpinWait.SpinUntil(() => _parameters != null, TimeSpan.FromSeconds(timeoutSeconds));
            }

            if (_parameters == null)
            {
                Trace.TraceError("No FileStoreParameters object provided at DicomServer initialization. Incoming DICOM requests will not be handled correctly.");
            }
            else if (_parameters?.DicomSaver == null)
            {
                Trace.TraceWarning("File Store service is created, but no image saver class provided. Incoming images will not be saved.");
            }

            return(_parameters);
        }
        /// <inheritdoc />
        /// <exception cref="DicomNetworkException">If the service is already listening.</exception>
        /// <exception cref="System.Net.Sockets.SocketException">If another service is already listening on this socket.</exception>
        public bool StartServer(int port, Func <IReadOnlyDictionary <DicomUID, DicomTransferSyntax[]> > getAcceptedTransferSyntaxes, TimeSpan timeout)
        {
            if (!ApplicationEntityValidationHelpers.ValidatePort(port))
            {
                throw new ArgumentException("The port is not valid.", nameof(port));
            }

            // Check if we are already listening
            if (IsListening)
            {
                throw new DicomNetworkException("We are already listening. Please call stop server before starting.");
            }

            // Looks like StartServer has been called before but failed to start listening.
            // Lets dispose of the current instance and try again.
            if (_dicomServer != null)
            {
                DisposeDicomServer();
            }

            var fileStoreParameters = new DicomFileStoreParameters(DicomDataReceiverUpdate, getAcceptedTransferSyntaxes, _dicomSaver);

            // Preload dictionary to prevent timeouts
            DicomDictionary.EnsureDefaultDictionariesLoaded();

            // Constructing the listener dicom server will attempt to start the service.
            _dicomServer = DicomServer.Create <ListenerDicomService>(ipAddress: "localhost", port: port, userState: fileStoreParameters);

            // Wait until listening or we have an exception.
            SpinWait.SpinUntil(() => _dicomServer.IsListening || _dicomServer.Exception != null, timeout);

            if (_dicomServer.Exception != null)
            {
                // Throw any exceptions
                throw _dicomServer.Exception;
            }

            return(_dicomServer?.IsListening ?? false);
        }