Maybe you want to try netcat with -p option, if you don't want to write code by yourself, example: $ nc -p 31337 www.google.com 80
Here is the explanation for "-p" option from man page: Specifies the source port nc should use, subject to privilege restrictions and availability. It is an error to use this option in conjunction with the -l option.
Note though to use any port under 1024 requires root permission.