I think the reason for your header already sent error is that you are using
comments_number()
that will echo the value.
Try instead
get_comments_number( $post_id );
to return it.
The Codex info on this function says that it returns the total number of comments, trackbacks, and pingbacks for the post.