title: C# - Socket to log on to Firewall
body: <p>I wrote an app to automatically connect to our different Firewalls. All of them work with the same frontend. We telnet to the IP and they give the message LOGIN or LOGOUT and ask for a username or password.</p>
  
  <p>I used this code: </p>
  
  <pre><code>    public static void ConnectToFirewall(string strUsername, string strPassword, string strFirewallIp)
      {
          IPAddress[] ipaIpAddressCollection = Dns.GetHostAddresses(strFirewallIp);
          IPEndPoint ipeIpEndPoint = new IPEndPoint(ipaIpAddressCollection[0], intPort);
          Socket sckSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
          sckSocket.Connect(ipeIpEndPoint);
          string strData = strUsername + "\r\n"+ strPassword + "\r\n";
          byte[] bytData = new byte[1024];
          bytData = Encoding.ASCII.GetBytes(strData);
          sckSocket.Send(bytData);
          byte[] bytDataReceived = new byte[1024];
          int intData = sckSocket.Receive(bytDataReceived);
          sckSocket.Close();
      }
  </code></pre>
  
  <p>If I am not logged in, when I telnet to it, I receive the message: LOGIN, username: / Password:.
  If I am logged on, I receive LOGOUT, username: / Password.</p>
  
  <p>This works perfectly well with the above method for half of my firewalls, it does not seem to work (I keep getting login as if I had not tried to pass credentials). 
  I also tried it with</p>
  
  <pre><code>    TcpClient tcpConnection = new TcpClient(myip,myport);
  </code></pre>
  
  <p>but this gives the same result. It works for certain firewall ip's. fails for others. They are all in the same domain.</p>
  
  <p>Does anyone have idea how I could get past this or what steps I could undertake to troubleshoot this or what may be the cause of some server not accepting this method, allthough it does accept if I telnet to it?</p>
  
  <p>Any help or suggestions are appreciated.</p>
  
  <p>Many thanks in advance.</p>