package de.gesellix.docker.client.filesocket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.SocketException;

public class NamedPipeSocket extends FileSocket {

    private static final Logger log = LoggerFactory.getLogger(NamedPipeSocket.class);

    private RandomAccessFile namedPipe = null;
    private boolean closed = false;

    @Override
    public void connect(SocketAddress endpoint, int timeout) throws IOException {
        if (!InetSocketAddress.class.isInstance(endpoint)) {
            throw new IllegalArgumentException("Expected endpoint to be a InetSocketAddress");
        }

        InetSocketAddress inetSocketAddress = (InetSocketAddress) endpoint;
        InetAddress address = inetSocketAddress.getAddress();
        String socketPath = decodeHostname(address);
        log.debug("connect via '{}'...", socketPath);

        socketPath = socketPath.replace("/", "\\\\");
        this.namedPipe = new RandomAccessFile(socketPath, "rw");
    }

    @Override
    public InputStream getInputStream() throws IOException {
        if (namedPipe == null) {
            throw new SocketException("Socket is not initialized");
        }
        return new FileInputStream(namedPipe.getFD());
    }

    @Override
    public OutputStream getOutputStream() throws IOException {
        if (namedPipe == null) {
            throw new SocketException("Socket is not initialized");
        }
        return new FileOutputStream(namedPipe.getFD());
    }

    @Override
    public boolean isClosed() {
        return closed;
    }

    @Override
    public void close() throws IOException {
        if (namedPipe != null) {
            synchronized (this) {
                namedPipe.close();
            }
        }
        closed = true;
    }
}

@@ -408,16 +408,66 @@
 ception;
+%0Aimport java.util.concurrent.atomic.AtomicBoolean;
 %0A%0Apublic
@@ -657,17 +657,29 @@
 private 
-b
+final AtomicB
 oolean c
@@ -690,13 +690,32 @@
 d = 
+new AtomicBoolean(
 false
+)
 ;%0A%0A 
@@ -1914,16 +1914,22 @@
 n closed
+.get()
 ;%0A    %7D%0A
@@ -1980,32 +1980,184 @@
 s IOException %7B%0A
+        if (!closed.compareAndSet(false, true)) %7B%0A            // if compareAndSet() returns false closed was already true%0A            return;%0A        %7D%0A
         if (name

Use a safer method to recognize a closed Socket