public static void relayInOneThread(InputStream in1, OutputStream out1, InputStream in2, OutputStream out2, final int bufferSize) {
        long byteCounterForLog1 = 0;
        long byteCounterForLog2 = 0;
        try{
            byte[] buffer = new byte[bufferSize];
            boolean tryToClose = false;
            while(true) {
                boolean action = false;

                // data from in1?
                try {
                    if (in1.available()>0) {
                        int cc = in1.read(buffer);
                        byteCounterForLog1 += cc;
                        log.info(" > "+cc+" bytes ("+byteCounterForLog1+" bytes total)");
                        out1.write(buffer,0,cc);
                        out1.flush();
                        action = true;
                    }
                } catch (IOException e) {
                    log.fine("relay1: "+e.toString());
                    tryToClose = true;
                }

                // data from in2?
                try {
                    if (in2.available()>0) {
                        int cc = in2.read(buffer);
                        byteCounterForLog2 += cc;
                        log.info(" < "+cc+" bytes ("+byteCounterForLog2+" bytes total)");
                        out2.write(buffer,0,cc);
                        out2.flush();
                        action = true;
                    }
                } catch (IOException e) {
                    log.fine("relay2: "+e.toString());
                    tryToClose = true;
                }
                
                // close?
                if (!action && tryToClose) {
                    // yes
                    in1.close();
                    in2.close();
                    out1.close();
                    out2.close();
                    break;
                }
                    
                // rest a bit, if no action
                if (!action) {
                    Thread.sleep(SLEEP_ON_INACTIVITY_MS);
                }
            }
        }
        catch(Exception e) {
            log.log(Level.WARNING, "connection interrupted", e);
        }
    }