System.IO.Pipes.NamedPipeServerStream.WaitForConnectionAsyncCore C# (CSharp) Method

WaitForConnectionAsyncCore() private method

private WaitForConnectionAsyncCore ( ) : Task
return Task
        private async Task WaitForConnectionAsyncCore()
        {   
            // This is the same implementation as is in WaitForConnection(), but using Socket.AcceptAsync
            // instead of Socket.Accept.
             
            // Binding to an existing path fails, so we need to remove anything left over at this location.
            // There's of course a race condition here, where it could be recreated by someone else between this
            // deletion and the bind below, in which case we'll simply let the bind fail and throw.
            Interop.Sys.Unlink(_path); // ignore any failures
            var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
            try
            {
                socket.Bind(new UnixDomainSocketEndPoint(_path));
                socket.Listen(1);

                Socket acceptedSocket = await socket.AcceptAsync().ConfigureAwait(false);
                SafePipeHandle serverHandle = new SafePipeHandle(acceptedSocket);
                ConfigureSocket(acceptedSocket, serverHandle, _direction, _inBufferSize, _outBufferSize, _inheritability);

                InitializeHandle(serverHandle, isExposed: false, isAsync: (_options & PipeOptions.Asynchronous) != 0);
                State = PipeState.Connected;
            }
            finally
            {
                // Bind will have created a file.  Now that the client is connected, it's no longer necessary, so get rid of it.
                Interop.Sys.Unlink(_path); // ignore any failures; worst case is we leave a tmp file

                // Clean up the listening socket
                socket.Dispose();
            }
        }