Yes, use the bind() method. This mirrors the bind() function available in most C-level socket implementations. Note that you can't always choose freely which port to use, on some system some ranges are reserved and considered off-limits to user applications.