Inconsistent behavior of StreamReader
I have written a function to check if the SMTP Server is active or not using TCPClient. When I stop the SMTP Service, the function very well returns me its status as inactive, but when the SMTP service is running, then "occassionally" the Peek() method throws an exception, that falsely return the status of SMTP server as inactive. Can anybody tell me why is the behavior of these function so inconsistent and any ideas on how to get rid of this problem..
private bool IsSMTPActive()
{
try
{
client = new TcpClient("10.135.158.97", 25);
ns = client.GetStream();
stdIn = new StreamReader(ns);
stdOut = new StreamWriter(ns);
stdOut.WriteLine("HELO " + Dns.GetHostName());
stdOut.Flush();
int responseCode = GetResponse(stdIn);
if (responseCode != 220)
{
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
return false;
}
}
static int GetResponse(StreamReader stdIn)
{
try
{
string response = "";
while (stdIn.Peek() != -1)
{
response += stdIn.ReadLine() + "\r\n";
Thread.Sleep(100);
}
return Convert.ToInt32(response.Substring(0, 3));
}
catch
{
return 0;
}
}

