void CContextControl::CreateTab()
{
	wxGetApp().AddStartupProfileRecord(_T("CContextControl::CreateTab"));
	Freeze();

	CState* pState = 0;

	// See if we can reuse an existing context
	for (size_t i = 0; i < m_context_controls.size(); i++)
	{
		if (m_context_controls[i].tab_index != -1)
			continue;

		if (m_context_controls[i].pState->IsRemoteConnected() ||
			!m_context_controls[i].pState->IsRemoteIdle())
			continue;

		pState = m_context_controls[i].pState;
		m_context_controls.erase(m_context_controls.begin() + i);
		if (m_current_context_controls > (int)i)
			m_current_context_controls--;
		break;
	}
	if (!pState)
	{
		pState = CContextManager::Get()->CreateState(m_pMainFrame);
		if (!pState->CreateEngine())
		{
			wxMessageBoxEx(_("Failed to initialize FTP engine"));
		}
	}

	// Restore last server and path
	CServer last_server;
	CServerPath last_path;
	if (COptions::Get()->GetLastServer(last_server) && last_path.SetSafePath(COptions::Get()->GetOption(OPTION_LASTSERVERPATH)))
		pState->SetLastServer(last_server, last_path);

	CreateContextControls(pState);

	pState->GetRecursiveOperationHandler()->SetQueue(m_pMainFrame->GetQueue());

	wxString localDir = COptions::Get()->GetOption(OPTION_LASTLOCALDIR);
	if (!pState->SetLocalDir(localDir))
		pState->SetLocalDir(_T("/"));

	CContextManager::Get()->SetCurrentContext(pState);

	if (!m_pMainFrame->RestoreSplitterPositions())
		m_pMainFrame->SetDefaultSplitterPositions();

	if (m_tabs)
		m_tabs->SetSelection(m_tabs->GetPageCount() - 1);

	Thaw();
}