BitSharp.Network.Peer.ConnectAsync C# (CSharp) Method

ConnectAsync() public method

public ConnectAsync ( ) : Task
return Task
        public async Task ConnectAsync()
        {
            // take the lock to see if a connect can be started
            lock (this.objectLock)
            {
                if (this.isDisposed)
                    return;

                // don't connect if already connected, or started connecting elsewhere
                if (this.IsConnected || this.startedConnecting)
                    return;

                // indicate that connecting will be started
                this.startedConnecting = true;
            }

            // start the connection
            try
            {
                await Task.Factory.FromAsync(this.socket.BeginConnect(this.RemoteEndPoint, null, null), this.socket.EndConnect);

                this.LocalEndPoint = (IPEndPoint)this.socket.LocalEndPoint;
                this.IsConnected = true;
            }
            catch (Exception ex)
            {
                logger.Debug(ex, $"Error on connecting to {RemoteEndPoint}");
                Disconnect(ex);

                throw;
            }
            finally
            {
                // ensure started connecting flag is cleared
                this.startedConnecting = false;
            }
        }