Ethernet Shield won't connect but works fine as a server

I'm trying to get my Ethernet shield to connect to my web server. I have tested using the shield as its own server, and that works perfectly fine. I have also verified (using telnet) that the main server is working properly, and it is. The Arduino can't seem to connect to the server, however (or any server for that matter). My code is as follows:

#include <Dhcp.h>
#include <Dns.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>

#define PIN_TRIG 41
#define PIN_ECHO 39

byte MAC[] = { 0x13, 0xD9, 0xB8, 0x76, 0xE3, 0x68 };
IPAddress IP(140, 192, 242, 253);
IPAddress DNS(8, 8, 8, 8);
IPAddress GATEWAY(140, 192, 242, 1);

byte SERVER[] = { 65, 60, 27, 27 };
EthernetClient client;

void setup()
{
  pinMode(PIN_TRIG, OUTPUT);
  pinMode(PIN_ECHO, INPUT);
  Serial.begin(9600);
  Serial.println("Initializing Ethernet shield...");
  Ethernet.begin(MAC, IP, DNS, GATEWAY);
  delay(1000);
  Serial.println("Ethernet shield initialized.");
}

float getRange()
{
  digitalWrite(PIN_TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(PIN_TRIG, LOW);
  int duration = pulseIn(PIN_ECHO, HIGH);
  float inches = ((float) duration) / 148.0f;
  return inches;
}

void loop()
{
  int status = client.connect(SERVER, 2048);
  if (status == 1)
  {
    Serial.println("Connected.");
    Serial.println("Sending hello to server...");
    client.println("TEST:HELLO");
    client.stop();
  }
  else
  {
    Serial.print("Could not connect to server: error ");
    Serial.println(status);
  }
  
  delay(2500);
}

It prints "Could not connect to server: error 0" to the serial console every 2.5 seconds. I modified EthernetClient.cpp to change its return values to figure out where the 0 was coming from, and I've narrowed it down to EthernetClient.cpp:66:

62 while (status() != SnSR::ESTABLISHED) {
63     delay(1);
64     if (status() == SnSR::CLOSED) {
65       _sock = MAX_SOCK_NUM;
66       return 0;
67     }
68   }

It appears as though the Arduino-side socket is immediately disconnecting directly after the connection attempt. Again, I've verified that a telnet client can connect and remain connected to the server, and communication works both directions, so it's not a server issue. Anybody have a clue as to why the shield is having such a hard time connecting?

This section of code shows you are trying to connect on port 2048. Most web servers listen on port 80.

  int status = client.connect(SERVER, 2048);

Basic client test code you can try to see if you get an error response (server was shut down for good at midnight).

//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;

//////////////////////

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.1"); //download text
    client.println("Host: web.comporium.net");
    client.println("Connection: close");  //close 1.1 persistent connection  
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

SurferTim:
This section of code shows you are trying to connect on port 2048. Most web servers listen on port 80.

  int status = client.connect(SERVER, 2048);

I'm connecting to a custom plaintext-TCP server running on remote port 2048. I'm able to telnet into said port and send and receive data with a computer on the same switch as the Arduino.

zoomkat:
Basic client test code you can try to see if you get an error response (server was shut down for good at midnight).

I've been having issues with DHCP as well. I'm pretty sure my school has multiple DHCP servers running on its network, which (due to a known bug I found documented elsewhere on the site) confuses the shield when it tries to request an IP address allocation. Statically allocating the IP address worked when using the Arduino as a server, but not as a client.

It sounds like your issue may be with your network, not your arduino. Is there a web server within the school, preferably in the 140. 192. 242.x range that you can run a test against?

wildbill:
It sounds like your issue may be with your network, not your arduino. Is there a web server within the school, preferably in the 140. 192. 242.x range that you can run a test against?

I can try tomorrow. I don't understand how two computers on the same network switch as the Arduino (and thus same subnet) can connect to the server but the Arduino can't, but I can try connecting through the switch directly to my laptop tomorrow.

I'm starting to think wildbill is right. It may be your network settings. The 140.x.x.x IPs are not a private IP range. Are you sure those network settings are correct? Check the network settings on the computers that can connect ok. Have you tried getting the network stuff with DHCP?

Also insure there is not a duplicate IP or mac address on the localnet.

edit: Here is a short sketch that should display your localnet settings using DHCP:

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

byte mac[] = {  0x00, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

void setup() {
  Serial.begin(9600);

  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.print(F("Starting ethernet..."));
  if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  else {
      Serial.print(F("IP: "));
      Serial.println(Ethernet.localIP());
      Serial.print(F("Subnet: "));
      Serial.println(Ethernet.subnetMask());
      Serial.print(F("Gateway: "));
      Serial.println(Ethernet.gatewayIP());
      Serial.print(F("DNS server: "));
      Serial.println(Ethernet.dnsServerIP());
}

void loop() {
}