static new Map getBotPort_cache; static int getBotPort_timeout = 5000; // returns 0 if not found static int getBotPort(S searchPattern) { Integer port = getBotPort_cache.get(searchPattern); if (port != null) try { DialogIO io = talkTo("localhost", port); io.waitForLine(/*getBotPort_timeout*/); // TODO: implement S line = io.readLineNoBlock(); if (indexOfIgnoreCase(line, searchPattern) == 0) { io.close(); return port; } } catch (Exception e) { e.printStackTrace(); } for (ProgramScan.Program p : quickBotScan()) if (indexOfIgnoreCase(p.helloString, searchPattern) == 0) { // strict matching - start of hello string only, but case-insensitive getBotPort_cache.put(searchPattern, p.port); return p.port; } return 0; }