'C# SFTP SSH.NET socket read operation has timed out after 30000 milliseconds

I am receiving random errors when uploading my file to SFTP. Say I have 100 files to upload, out of which 90-95 files are loading successful and rest 4-5 files are throwing error as below. Not sure what is causing the issue. Below is the code. It is failing at the step client.Connect();

Error Message: FileUpload Repository Error: Renci.SshNet.Common.SshOperationTimeoutException: Socket read operation has timed out after 30000 milliseconds. at Renci.SshNet.Abstractions.SocketAbstraction.Read(Socket socket, Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)

int port = 8022;
                if (int.TryParse(portString, out port))
                {
                    using (var client = new SftpClient(host, port, username, pd))
                    {
                        client.Connect();

                        if (!client.IsConnected)
                        {
                            throw new Exception("connection to the SFTP failed");
                        }

                        client.ChangeDirectory(FinalFolder);
                       
                        using (var fs = new FileStream(sourcefile, FileMode.Open))
                        {
                            client.BufferSize = 4 * 1024;
                            client.UploadFile(fs, Path.GetFileName(sourcefile));
                            fs.Close();                          
                                                       
                            
                        }
                    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source