What can someone do if i write a program to open a TCP port and run, lets say an echo server? Can some one access data on my system? or any other risk of opening port?
It depends on how well the program is written and under what credentials your server program runs as. For instance say you allocate a fixed-length buffer to read from the socket, and then you read in more than the allocated size, it might be possible to craft a server requests that overruns the buffer on the stack and overwrites the return address. At that point the hacker could re-point the return address to be on the (overrun) buffer, which could contain actual code. Then, if your server program runs as root/administrator you're letting the code being executed in that context.
If your program is vulnerable, a hacker could possibly execute code on your system with privileges of your program. Or perhaps he could arrange a DoS attack (your program would consume all CPU or memory or disk space and everything would stop working and collapse :-) ).
In the past one also could find a bug in the OS TCP/IP stack and exploit it. So even if your program was perfect, there was a chance of getting into troubles with an open port. However I haven't heard of such bugs for a while now.