OKay, this works for me now. Maybe I can help someone coming from google or from somewhere else....
- (NSNumber*) getPortnumber{
int port = -1;
CFSocketContext context;
memset(&context, 0, sizeof(context));
context.info = (__bridge void*)self;
// Get Socket native handle from existing stream
CFDataRef socketData = CFWriteStreamCopyProperty((__bridge CFWriteStreamRef)(_writeStream), kCFStreamPropertySocketNativeHandle);
// Get the native handle
CFSocketNativeHandle handle;
CFDataGetBytes(socketData, CFRangeMake(0, sizeof(CFSocketNativeHandle)), &handle);
// Get the socket
CFSocketRef cfSocket = CFSocketCreateWithNative(NULL, handle, kCFSocketNoCallBack, nil, &context);
// CFSocketCopyAddress will return the address of the socket
CFDataRef dataRef = CFSocketCopyAddress(cfSocket);
struct sockaddr_in *sockaddr = (struct sockaddr_in*)CFDataGetBytePtr(dataRef);
// Get the port from the address information
port = sockaddr->sin_port;
// Output Objective-C friendly =)
NSNumber *portnr = [NSNumber numberWithInt:port];
return portnr;
}