private bool ConnectToShare(string uncSharePath, out uint treeId) { treeId = 0; string serverName = Smb2Utility.GetServerName(uncSharePath); var serverIPs = Dns.GetHostEntry(serverName).AddressList; if (serverIPs == null || serverIPs.Length == 0) { return(false); } uint status = 0; foreach (var ip in serverIPs) // Try to connect to each IP since not all of them can be connected successfully. { try { client = new Smb2FunctionalClient(testConfig.Timeout, testConfig, Site); client.ConnectToServer(Smb2TransportType.Tcp, serverName, ip); status = client.Negotiate( Smb2Utility.GetDialects(testConfig.MaxSmbVersionSupported), true, checker: (header, response) => { }); if (status != Smb2Status.STATUS_SUCCESS) { continue; } status = client.SessionSetup(testConfig.DefaultSecurityPackage, serverName, testConfig.AccountCredential, false, checker: (header, response) => { }); if (status != Smb2Status.STATUS_SUCCESS) { client.Disconnect(); continue; } status = client.TreeConnect(uncSharePath, out treeId, checker: (header, response) => { }); if (status == Smb2Status.STATUS_SUCCESS) { return(true); } else { client.LogOff(); client.Disconnect(); } } catch { // Catch the exceptions thrown in Negotiage/SessionSetup/TreeConnect // Try next IP } } return(false); }