boolean (final String host, final int port) { if (myState.compareAndSet(State.DISCONNECTED, State.CONNECTING)) { boolean success = false; try { final Bootstrap bootstrap = new Bootstrap().group(myEventLoopGroup).channel(NioSocketChannel.class).handler(myChannelInitializer); bootstrap.option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true); final ChannelFuture future = bootstrap.connect(host, port).syncUninterruptibly(); success = future.isSuccess(); if (success) { myConnectFuture = future; try { onConnect(); } catch (Throwable e) { LOG.error(e); } } return success; } finally { myState.compareAndSet(State.CONNECTING, success? State.CONNECTED : State.DISCONNECTED); } } // already connected return true; }