Ethernet Client Working Sometimes Only

Hi guys
I am trying to use the Ethernet Client example. I first created a bridge between wifi and ethernet on laptop and tried it. It is only working sometimes. Then I connected my mega board with ethernet shield to a router and it is also working only some times.

I get the following outputs with the code. (Please note that you need to press ‘a’ in serial monitor to start the program).

Chandra

/*
  Web client

 This sketch connects to a website (http://www.google.com)
 using an Arduino Wiznet Ethernet shield.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe, based on work by Adrian McEwen

 */

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
//char server[] = "http://www.mcprakash.netau.net";    // name address for Google (using DNS)
char server[] = "www.arduino.cc";

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
char ch;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  
  while(1)
  {
    if(Serial.available()>0)
    {
     ch=Serial.read();
     if(ch=='a')
        break; 
      
    }
  }
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  Serial.print("Arduino IP is:");
  Serial.println(Ethernet.localIP());
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
 
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    /*
    client.println("GET /reemo_conf.txt HTTP/1.0");
    client.println("Host: http://mcprakash.netau.net/");
    //client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();
    */
    client.println("GET /latest.txt HTTP/1.1");
    client.println("Host: www.arduino.cc");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    while(1)
    {
      if(Serial.available()>0)
        {
          Serial.println("Exiting");
          exit(0);
        }
      
    }
  }
  
  
}

OUTPUT

Connected

Arduino IP is:192.168.1.77
connecting...
connected
HTTP/1.1 200 OK
Server: nginx/1.4.2
Content-Type: text/plain
Last-Modified: Wed, 02 Oct 2013 13:46:55 GMT
ETag: "524c23cf-5"
Content-Length: 5
Accept-Ranges: bytes
Date: Tue, 19 Aug 2014 16:43:14 GMT
X-Varnish: 1831322934
Age: 0
Via: 1.1 varnish
Connection: close
X-Cache: MISS
X-URL: /latest.txt

0105

disconnecting.

Connected but did not get the file

Arduino IP is:192.168.1.77
connecting...
connected

disconnecting.

Not Connected

Arduino IP is:192.168.1.77
connecting...
connection failed

disconnecting.