Serial commands to Arduino mega 2560

Hey all, I have read a lot of post about serial communication but its still not working here. On my arduino mega i have the following running:

int ledPin = 13;
int state=0;
void setup() {
    pinMode(ledPin, OUTPUT); // pin will be used to for output
    Serial.begin(9600); // same as in your c++ script
}

void loop() {
  if (Serial.available() > 0)
  {
    state = Serial.read(); // used to read incoming data

    switch(state)// see what was sent to the board
    {
      case '1': // if the the one was sent
        digitalWrite(ledPin,HIGH);
      break;
      case '0': // if 0 was sent
        digitalWrite(ledPin,LOW);
      break;
      default:
      break;
    }
  }
}

On my pc I have the Serial Class like its found here: see reply below

Then I wrote the next simple code:

#include "SerialClass.h"
#include < iostream >

int main( int argc, char **argv )
{
      Serial mySerial("COM4");
    std::cout << mySerial.IsConnected() << std::endl;
      system("PAUSE");
    return 0;
}

it compiles fine but when i run it it tells me it cant connect to COM4, while the ArduinoIDE can upload the code trough COM4.

Anybody know why its not working?

Cheers, Xander

I was not allowed to post the link in the first post: http://www.arduino.cc/playground/Interfacing/CPPWindows

Do you have the arduino serial port "on" when you try the code? Try running the code without the Arduino IDE running.

Yes, but I have already tried it when its not running. I know the its not going to work with the serial monitor on but I don't have that on either.

I believe there are problems with that serial wrapper. Folks have reported success with this one... http://www.codeproject.com/KB/system/serial.aspx