private void startServer(PrologInterface pif, PrologSession session) {
    try {
      String queryString =
          bT(
              PDTConsolePredicates.PDT_START_CONSOLE_SERVER,
              "Port",
              Util.quoteAtom(
                  PrologRuntimePlugin.getDefault().getPrologInterfaceRegistry().getKey(pif)));
      Debug.info("starting console server using: " + queryString);

      Map<String, ?> result = session.queryOnce(queryString);
      if (result == null) {
        Debug.info("starting server failed, which may mean that it is actualy running already.");
        result = session.queryOnce(bT(PDTConsolePredicates.PDT_CURRENT_CONSOLE_SERVER, "Port"));
        if (result == null) {
          throw new RuntimeException("No Server running.");
        }
      }

      int port = Integer.parseInt((String) result.get("Port"));
      Debug.debug("A server thread seems to be listinging at port " + port);
    } catch (Throwable e) {
      Debug.report(e);
      throw new RuntimeException(e);
    }
  }
  private void reconfigureViewer(final PrologInterface pif) {

    if (Display.getCurrent() != viewer.getControl().getDisplay()) {
      viewer
          .getControl()
          .getDisplay()
          .asyncExec(
              new Runnable() {
                @Override
                public void run() {
                  reconfigureViewer(pif);
                }
              });
      return;
    }
    if (pif == null) {

      viewer.setModel(null);
      viewer.setHistory(null);
      viewer.setCompletionProvider(null);
      title.setText("no console available(yet).");
      return;
    }

    ConsoleViewer.SavedState savedState = viewerStates.get(pif);
    if (savedState == null) {
      viewer.clearOutput();
      viewer.setModel(models.get(pif));
      PrologCompletionProvider completionProvider = new PrologCompletionProvider();
      completionProvider.setPrologInterface(pif);
      viewer.setCompletionProvider(completionProvider);
      ConsoleHistory history = new ConsoleHistory();
      viewer.setHistory(history);
      loadHistory(history);
    } else {
      viewer.loadState(savedState);
    }
    PrologInterfaceRegistry reg = PrologRuntimePlugin.getDefault().getPrologInterfaceRegistry();
    String key = reg.getKey(pif);
    title.setText(SelectContextPIFAutomatedAction.getLabelForPif(key, reg));
    //		Object configuration = pif.getAttribute(PrologRuntimeUI.CONFIGURATION_ATTRIBUTE);
    //		if (configuration == null) {
    //			title.setText(key);
    //		} else {
    //			title.setText(key + " (" + configuration.toString().replaceAll("&", "&&") + ")");
    //		}

    viewer.setEnterSendsSemicolon(false);
  }
  private void ensureConnection(final PrologInterface pif, PrologSocketConsoleModel model)
      throws PrologInterfaceException {
    if (model.isConnected()) {
      return;
    }

    PrologSession session = pif.getSession(PrologInterface.NONE);
    //		FileSearchPathConfigurator.configureFileSearchPath(PrologRuntimeUIPlugin.getDefault()
    //				.getLibraryManager(), session,
    //				new String[] { PDTConsole.PL_LIBRARY });

    Map<String, ?> result = null;
    try {
      //			result = session.queryOnce( "consult(lib_pdt_console_pl(loader)).");
      result =
          session.queryOnce(
              bT(
                  PDTConsolePredicates.PDT_START_CONSOLE_SERVER,
                  "Port",
                  Util.quoteAtom(
                      PrologRuntimePlugin.getDefault().getPrologInterfaceRegistry().getKey(pif))));
      if (result == null) {
        startServer(pif, session);
        result = session.queryOnce(bT(PDTConsolePredicates.PDT_CURRENT_CONSOLE_SERVER, "Port"));
      }
      if (result == null) {
        throw new RuntimeException("could not install console server");
      }
    } catch (Exception e) {
      Debug.info(e.toString());
    } finally {
      if (session != null) {
        session.dispose();
      }
    }

    int port = Integer.parseInt(result.get("Port").toString());
    model.setPort(port);
    model.connect();
  }