private void TryRegister(Connection connection) { if (!_listenerAddedConnection.CompareAndSet(null, connection)) { //already registering/registered to another connection return; } var clientMessage = ClientAddClusterViewListenerCodec.EncodeRequest(); void HandlePartitionsViewEvent(int version, ICollection <KeyValuePair <Guid, IList <int> > > partitions) => _partitionService.HandlePartitionsViewEvent(connection, partitions, version); void EventHandler(ClientMessage message) => ClientAddClusterViewListenerCodec.EventHandler.HandleEvent(message, HandleMembersViewEvent, HandlePartitionsViewEvent); IFuture <ClientMessage> future = _clientInvocationService.InvokeListenerOnConnection(clientMessage, EventHandler, connection); future.ToTask().ContinueWith(task => { if (!task.IsFaulted) { if (task.Result != null) { return; } } //completes with exception, listener needs to be re-registered TryReRegisterToRandomConnection(connection); }); }