int CallInAuthenticateUser::getUserSession()
	{
		SessionPtr currentSession;
		bool reconnectAllowed;

		if (!APP_CONTEXT.getPropertyManager()->getPropertyBool("session.reconnect", reconnectAllowed)) {
			reconnectAllowed = true;
		}

		if (reconnectAllowed)
		{
			currentSession = APP_CONTEXT.getSessionStore()->getFirstDisconnectedSessionUserName(mUserName, mDomainName);
			if (currentSession)
			{
				WLog_Print(logger_CallInAuthenticateUser, WLOG_DEBUG,
					"found disconnected session - sessionId=%lu, state=%lu",
					currentSession->getSessionId(), currentSession->getConnectState());
			}
		}

		if ((!currentSession) || (currentSession->getConnectState() != WTSDisconnected))
		{
			// create new Session for this request
			currentSession = APP_CONTEXT.getSessionStore()->createSession();

			WLog_Print(logger_CallInAuthenticateUser, WLOG_DEBUG,
				"creating new session - sessionId=%lu",
				currentSession->getSessionId());

			currentSession->setUserName(mUserName);
			currentSession->setDomain(mDomainName);

			if (!currentSession->generateUserToken())
			{
				WLog_Print(logger_CallInAuthenticateUser, WLOG_ERROR,
					"generateUserToken failed for user %s with domain %s",
					mUserName.c_str(), mDomainName.c_str());
				mResult = 1;// will report error with answer
				return 1;
			}

			if (!currentSession->generateEnvBlockAndModify())
			{
				WLog_Print(logger_CallInAuthenticateUser, WLOG_ERROR,
					"generateEnvBlockAndModify failed for user %s with domain %s",
					mUserName.c_str(), mDomainName.c_str());
				mResult = 1;// will report error with answer
				return 1;
			}
			std::string moduleConfigName;

			if (!APP_CONTEXT.getPropertyManager()->getPropertyString("module", moduleConfigName)) {
				moduleConfigName = "X11";
			}
			currentSession->setModuleConfigName(moduleConfigName);
		}
		else
		{
			WLog_Print(logger_CallInAuthenticateUser, WLOG_DEBUG,
				"connecting to disconnected session - sessionId=%lu",
				currentSession->getSessionId());

			currentSession->setConnectState(WTSConnectQuery);
		}

		UINT32 connectionId = APP_CONTEXT.getConnectionStore()->getConnectionIdForSessionId(mSessionId);
		APP_CONTEXT.getConnectionStore()->getOrCreateConnection(connectionId)->setAbout2SwitchSessionId(currentSession->getSessionId());

		if (currentSession->getConnectState() == WTSDown)
		{
			std::string pipeName;
			if (!currentSession->startModule(pipeName))
			{
				WLog_Print(logger_CallInAuthenticateUser, WLOG_ERROR,
					"ModuleConfig %s does not start properly for user %s in domain %s",
					currentSession->getModuleConfigName().c_str(), mUserName.c_str(), mDomainName.c_str());
				mResult = 1;// will report error with answer
				return 1;
			}
		}

		WLog_Print(logger_CallInAuthenticateUser, WLOG_DEBUG,
			"switching from session %lu to session %lu",
			mSessionId, currentSession->getSessionId());

		TaskSwitchToPtr switchToTask = TaskSwitchToPtr(new TaskSwitchTo());
		switchToTask->setConnectionId(connectionId);
		switchToTask->setServiceEndpoint(currentSession->getPipeName());
		switchToTask->setOldSessionId(mSessionId);
		switchToTask->setNewSessionId(currentSession->getSessionId());
		APP_CONTEXT.addTask(switchToTask);

		return 0;
	}