private void discriminateConnection(Socket localSocket) throws IOException, ShutdownException { boolean closeSocket = true; try { final Connector.ConnectDetails connectDetails = Connector.read(localSocket.getInputStream()); final SocketWrapper socketWrapper = new SocketWrapper(localSocket); socketWrapper.setAddress(connectDetails.getAddress()); // Possible minor race if the socket is closed between here... final ResourcePool.Closeable closeable = getSocketSet(connectDetails.getConnectionType()).add(socketWrapper); // .. and the time a listener is registered. Will pick up such a zombie // the next time we try to use the resource. socketWrapper.addClosedListener( new SocketWrapper.ClosedListener() { public void socketClosed() { closeable.close(); } }); // We did good. closeSocket = false; } catch (CommunicationException e) { try { m_exceptionQueue.queue(e); } catch (ThreadSafeQueue.ShutdownException shutdownException) { // Can happen due to race condition with shutdown, ignore. } } finally { if (closeSocket) { try { localSocket.close(); } catch (IOException ioException) { UncheckedInterruptedException.ioException(ioException); // Ignore. } } } }