Index: contrib/conf/ia32-qe.sh
===================================================================
--- contrib/conf/ia32-qe.sh	(revision 8fe2c9bdbf4cd735969137d973f2ce061bae7aa9)
+++ contrib/conf/ia32-qe.sh	(revision 119b46e45d903caf31b28eb1f2046fe108aa20c4)
@@ -1,3 +1,15 @@
 #!/bin/sh
+
+if [ -z "${QEMU_BINARY}" ] ; then
+	QEMU_BINARY="`which --tty-only qemu 2> /dev/null`"
+fi
+
+if [ -z "${QEMU_BINARY}" ] ; then
+	QEMU_BINARY="`which --tty-only qemu-system-i386 2> /dev/null`"
+fi
+
+if [ -z "${QEMU_BINARY}" ] ; then
+	echo "QEMU binary not found"
+fi
 
 DISK_IMG=hdisk.img
@@ -8,3 +20,3 @@
 fi
 
-qemu $@ -m 32 -hda "$DISK_IMG" -cdrom image.iso -boot d
+"${QEMU_BINARY}" "$@" -m 32 -hda "$DISK_IMG" -cdrom image.iso -boot d
Index: contrib/conf/net-qe.sh
===================================================================
--- contrib/conf/net-qe.sh	(revision 8fe2c9bdbf4cd735969137d973f2ce061bae7aa9)
+++ contrib/conf/net-qe.sh	(revision 119b46e45d903caf31b28eb1f2046fe108aa20c4)
@@ -1,16 +1,32 @@
 #!/bin/sh
+
+if [ -z "${QEMU_BINARY}" ] ; then
+	QEMU_BINARY="`which --tty-only qemu 2> /dev/null`"
+fi
+
+if [ -z "${QEMU_BINARY}" ] ; then
+	QEMU_BINARY="`which --tty-only qemu-system-x86_64 2> /dev/null`"
+fi
+
+if [ -z "${QEMU_BINARY}" ] ; then
+	QEMU_BINARY="`which --tty-only qemu-system-i386 2> /dev/null`"
+fi
+
+if [ -z "${QEMU_BINARY}" ] ; then
+	echo "QEMU binary not found"
+fi
 
 case "$1" in
 	ne2k)
 		shift
-		qemu $@ -device ne2k_isa,irq=5,vlan=0 -net user -boot d -redir udp:8080::8080 -redir udp:8081::8081 -redir tcp:8080::8080 -redir tcp:8081::8081 -cdrom image.iso
+		"${QEMU_BINARY}" "$@" -device ne2k_isa,irq=5,vlan=0 -net user -boot d -redir udp:8080::8080 -redir udp:8081::8081 -redir tcp:8080::8080 -redir tcp:8081::8081 -cdrom image.iso
 		;;
 	e1k)
 		shift
-		qemu $@ -device e1000,vlan=0 -net user -boot d -redir udp:8080::8080 -redir udp:8081::8081 -redir tcp:8080::8080 -redir tcp:8081::8081 -cdrom image.iso
+		"${QEMU_BINARY}" "$@" -device e1000,vlan=0 -net user -boot d -redir udp:8080::8080 -redir udp:8081::8081 -redir tcp:8080::8080 -redir tcp:8081::8081 -cdrom image.iso
 		;;
 	rtl8139)
 		shift
-		qemu $@ -device rtl8139,vlan=0 -net user -boot d -redir udp:8080::8080 -redir udp:8081::8081 -redir tcp:8080::8080 -redir tcp:8081::8081 -cdrom image.iso
+		"${QEMU_BINARY}" "$@" -device rtl8139,vlan=0 -net user -boot d -redir udp:8080::8080 -redir udp:8081::8081 -redir tcp:8080::8080 -redir tcp:8081::8081 -cdrom image.iso
 		;;
 	*)
Index: rnel/genarch/include/genarch/drivers/amdm37x_dispc/amdm37x_dispc.h
===================================================================
--- kernel/genarch/include/genarch/drivers/amdm37x_dispc/amdm37x_dispc.h	(revision 8fe2c9bdbf4cd735969137d973f2ce061bae7aa9)
+++ 	(revision )
@@ -1,394 +1,0 @@
-/*
- * Copyright (c) 2012 Jan Vesely
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-/** @addtogroup genarch
- * @{
- */
-/**
- * @file
- * @brief Texas Instruments AM/DM37x SDRAM Memory Scheduler.
- */
-
-#ifndef KERN_AMDM37x_DISPC_H_
-#define KERN_AMDM37x_DISPC_H_
-
-/* AMDM37x TRM p. 1813 */
-#define AMDM37x_DISPC_BASE_ADDRESS 0x48050400
-#define AMDM37x_DISPC_SIZE 1024
-
-#define __paddname(line) PADD32_ ## line
-#define _paddname(line) __paddname(line)
-#define PADD32(count) uint32_t _paddname(__LINE__)[count]
-
-#include <typedefs.h>
-
-typedef struct {
-	const ioport32_t revision;
-#define AMDM37X_DISPC_REVISION_MASK  0xff
-
-	PADD32(3);
-	ioport32_t sysconfig;
-#define AMDM37X_DISPC_SYSCONFIG_AUTOIDLE_FLAG  (1 << 0)
-#define AMDM37X_DISPC_SYSCONFIG_SOFTRESET_FLAG  (1 << 1)
-#define AMDM37X_DISPC_SYSCONFIG_ENWAKEUP_FLAG  (1 << 2)
-#define AMDM37X_DISPC_SYSCONFIG_SIDLEMODE_MASK  0x3
-#define AMDM37X_DISPC_SYSCONFIG_SIDLEMODE_SHIFT  3
-#define AMDM37X_DISPC_SYSCONFIG_CLOCKACTIVITY_MASK  0x3
-#define AMDM37X_DISPC_SYSCONFIG_CLOCKACTIVITY_SHIFT  8
-#define AMDM37X_DISPC_SYSCONFIG_MIDLEMODE_MASK  0x3
-#define AMDM37X_DISPC_SYSCONFIG_MIDLEMODE_SHIFT  12
-
-	const ioport32_t sysstatus;
-#define AMDM37X_DISPC_SYSSTATUS_RESETDONE_FLAG  (1 << 0)
-
-	ioport32_t irqstatus;
-	ioport32_t irqenable;
-#define AMDM37X_DISPC_IRQ_FRAMEDONE_FLAG  (1 << 0)
-#define AMDM37X_DISPC_IRQ_VSYNC_FLAG  (1 << 1)
-#define AMDM37X_DISPC_IRQ_EVSYNCEVEN_FLAG  (1 << 2)
-#define AMDM37X_DISPC_IRQ_EVSYNCODD_FLAG  (1 << 3)
-#define AMDM37X_DISPC_IRQ_ACBIASCOUNTSTATUS_FLAG  (1 << 4)
-#define AMDM37X_DISPC_IRQ_PROGRAMMEDLINENUMBER_FLAG  (1 << 5)
-#define AMDM37X_DISPC_IRQ_GFXFIFOUNDERFLOW_FLAG  (1 << 6)
-#define AMDM37X_DISPC_IRQ_GFXENDWINDOW_FLAG  (1 << 7)
-#define AMDM37X_DISPC_IRQ_PALETTEGAMMALOADING_FLAG  (1 << 8)
-#define AMDM37X_DISPC_IRQ_OPCERROR_FLAG  (1 << 9)
-#define AMDM37X_DISPC_IRQ_VID1FIFOUNDERFLOW_FLAG  (1 << 10)
-#define AMDM37X_DISPC_IRQ_VID1ENDWINDOW_FLAG  (1 << 11)
-#define AMDM37X_DISPC_IRQ_VID2FIFOUNDERFLOW_FLAG  (1 << 12)
-#define AMDM37X_DISPC_IRQ_VID2ENDWINDOW_FLAG  (1 << 13)
-#define AMDM37X_DISPC_IRQ_SYNCLOST_FLAG  (1 << 14)
-#define AMDM37X_DISPC_IRQ_SYNCLOSTDIGITAL_FLAG  (1 << 15)
-#define AMDM37X_DISPC_IRQ_WAKEUP_FLAG  (1 << 16)
-
-	PADD32(8);
-	ioport32_t control;
-#define AMDM37X_DISPC_CONTROL_LCD_ENABLE_FLAG  (1 << 0)
-#define AMDM37X_DISPC_CONTROL_DIGITAL_ENABLE_FLAG  (1 << 1)
-#define AMDM37X_DISPC_CONTROL_MONOCOLOR_FLAG  (1 << 2)
-#define AMDM37X_DISPC_CONTROL_STNTFT_FLAG  (1 << 3)
-#define AMDM37X_DISPC_CONTROL_M8B_FLAG  (1 << 4)
-#define AMDM37X_DISPC_CONTROL_GOLCD_FLAG  (1 << 5)
-#define AMDM37X_DISPC_CONTROL_GODIGITAL_FLAG  (1 << 6)
-#define AMDM37X_DISPC_CONTROL_STDITHERENABLE_FLAG  (1 << 7)
-#define AMDM37X_DISPC_CONTROL_TFTDATALINES_MASK  0x3
-#define AMDM37X_DISPC_CONTROL_TFTDATALINES_SHIFT  8
-#define AMDM37X_DISPC_CONTROL_TFTDATALINES_12B  0
-#define AMDM37X_DISPC_CONTROL_TFTDATALINES_16B  1
-#define AMDM37X_DISPC_CONTROL_TFTDATALINES_18B  2
-#define AMDM37X_DISPC_CONTROL_TFTDATALINES_24B  3
-#define AMDM37X_DISPC_CONTROL_STALLMODE_FLAG  (1 << 11)
-#define AMDM37X_DISPC_CONTROL_OVERLAYOPTIMIZATION_FLAG  (1 << 12)
-#define AMDM37X_DISPC_CONTROL_GPIN0_FLAG  (1 << 13)
-#define AMDM37X_DISPC_CONTROL_GPIN1_FLAG  (1 << 14)
-#define AMDM37X_DISPC_CONTROL_GPOUT0_FLAG  (1 << 15)
-#define AMDM37X_DISPC_CONTROL_GPOUT1_FLAG  (1 << 16)
-#define AMDM37X_DISPC_CONTROL_HT_MASK  0x7
-#define AMDM37X_DISPC_CONTROL_HT_SHIFT  17
-#define AMDM37X_DISPC_CONTROL_TDMENABLE_FLAG  (1 << 20)
-#define AMDM37X_DISPC_CONTROL_TDMPARALLELMODE_MASK  0x3
-#define AMDM37X_DISPC_CONTROL_TDMPARELLELMODE_SHIFT  21
-#define AMDM37X_DISPC_CONTROL_TDMCYCLEFORMAT_MASK  0x3
-#define AMDM37X_DISPC_CONTROL_TDMCYCLEFORMAT_SHIFT  23
-#define AMDM37X_DISPC_CONTROL_TDMUNUSEDBITS_MASK  0x3
-#define AMDM37X_DISPC_CONTROL_TDMUNUSEDBITS_SHIFT  25
-#define AMDM37X_DISPC_CONTROL_PCKFREEENABLE_FLAG  (1 << 27)
-#define AMDM37X_DISPC_CONTROL_LCDENABLESIGNAL_FLAG  (1 << 28)
-#define AMDM37X_DISPC_CONTROL_KCDENABLEPOL_FLAG  (1 << 29)
-#define AMDM37X_DISPC_CONTROL_SPATIALTEMPORALDITHERINGFRAMES_MASK  0x3
-#define AMDM37X_DISPC_CONTROL_SPATIALTEMPORALDITHERINGFRAMES_SHIFT  30
-
-	ioport32_t config;
-#define AMDM37X_DISPC_CONFIG_PIXELGATED_FLAG  (1 << 0)
-#define AMDM37X_DISPC_CONFIG_LOADMODE_MASK  0x3
-#define AMDM37X_DISPC_CONFIG_LOADMODE_SHIFT  1
-#define AMDM37X_DISPC_CONFIG_LOADMODE_PGDATAEVERYFRAME  0x0
-#define AMDM37X_DISPC_CONFIG_LOADMODE_PGUSER  0x1
-#define AMDM37X_DISPC_CONFIG_LOADMODE_DATAEVERYFRAME  0x2
-#define AMDM37X_DISPC_CONFIG_LOADMODE_PGDFIRSTFRAME  0x3
-#define AMDM37X_DISPC_CONFIG_PALETTEGAMMATABLE_FLAG  (1 << 3)
-#define AMDM37X_DISPC_CONFIG_PIXELDATAGATED_FLAG  (1 << 4)
-#define AMDM37X_DISPC_CONFIG_PIXELCLOCKGATED_FLAG  (1 << 5)
-#define AMDM37X_DISPC_CONFIG_HSYNCGATED_FLAG  (1 << 6)
-#define AMDM37X_DISPC_CONFIG_VSYNCGATED_FLAG  (1 << 7)
-#define AMDM37X_DISPC_CONFIG_ACBIASGATED_FLAG  (1 << 8)
-#define AMDM37X_DISPC_CONFIG_FUNCGATED_FLAG  (1 << 9)
-#define AMDM37X_DISPC_CONFIG_TCKLCDENABLE_FLAG  (1 << 10)
-#define AMDM37X_DISPC_CONFIG_TCKLCDSELECTION_FLAG  (1 << 11)
-#define AMDM37X_DISPC_CONFIG_TCKDIGENABLE_FLAG  (1 << 12)
-#define AMDM37X_DISPC_CONFIG_TCKDIGSELECTION_FLAG  (1 << 13)
-#define AMDM37X_DISPC_CONFIG_FIFOMERGE_FLAG  (1 << 14)
-#define AMDM37X_DISPC_CONFIG_CPR_FLAG  (1 << 15)
-#define AMDM37X_DISPC_CONFIG_FIFOHANDCHECK_FLAG  (1 << 16)
-#define AMDM37X_DISPC_CONFIG_FIFOFILLING_FLAG  (1 << 17)
-#define AMDM37X_DISPC_CONFIG_LCDPALPHABLENDERENABLDE_FLAG  (1 << 18)
-#define AMDM37X_DISPC_CONFIG_TVALPHABLENDERENABLE_FLAG  (1 << 19)
-
-	PADD32(1);
-	ioport32_t default_color[2];
-	ioport32_t trans_color[2];
-#define AMDM37X_DISPC_COLOR_MASK 0xffffff
-
-	const ioport32_t line_status;
-	ioport32_t line_number;
-#define AMDM37X_DISPC_LINE_NUMBER_MASK 0x3ff
-
-	ioport32_t timing_h;
-#define AMDM37X_DISPC_TIMING_H_HSW_MASK 0xff
-#define AMDM37X_DISPC_TIMING_H_HSW_SHIFT 0
-#define AMDM37X_DISPC_TIMING_H_HFP_MASK 0xfff
-#define AMDM37X_DISPC_TIMING_H_HFP_SHIFT 8
-#define AMDM37X_DISPC_TIMING_H_HBP_MASK 0xfff
-#define AMDM37X_DISPC_TIMING_H_HBP_SHIFT 20
-
-	ioport32_t timing_v;
-#define AMDM37X_DISPC_TIMING_V_VSW_MASK 0xff
-#define AMDM37X_DISPC_TIMING_V_VSW_SHIFT 0
-#define AMDM37X_DISPC_TIMING_V_VFP_MASK 0xfff
-#define AMDM37X_DISPC_TIMING_V_VFP_SHIFT 8
-#define AMDM37X_DISPC_TIMING_V_VBP_MASK 0xfff
-#define AMDM37X_DISPC_TIMING_V_VBP_SHIFT 20
-
-	ioport32_t pol_freq;
-#define AMDM37X_DISPC_POL_FREQ_ACB_MASK  0xff
-#define AMDM37X_DISPC_POL_FREQ_ACB_SHIFT 0
-#define AMDM37X_DISPC_POL_FREQ_ACBI_MASK  0xf
-#define AMDM37X_DISPC_POL_FREQ_ACBI_SHIFT 8
-#define AMDM37X_DISPC_POL_FREQ_IVS_FLAG  (1 << 12)
-#define AMDM37X_DISPC_POL_FREQ_IHS_FLAG  (1 << 13)
-#define AMDM37X_DISPC_POL_FREQ_IPC_FLAG  (1 << 14)
-#define AMDM37X_DISPC_POL_FREQ_IEO_FLAG  (1 << 15)
-#define AMDM37X_DISPC_POL_FREQ_RF_FLAG  (1 << 16)
-#define AMDM37X_DISPC_POL_FREQ_ONOFF_FLAG  (1 << 17)
-
-	ioport32_t divisor;
-#define AMDM37X_DISPC_DIVISOR_PCD_MASK  0xff
-#define AMDM37X_DISPC_DIVISOR_PCD_SHIFT  0
-#define AMDM37X_DISPC_DIVISOR_LCD_MASK  0xff
-#define AMDM37X_DISPC_DIVISOR_LCD_SHIFT  16
-
-	ioport32_t global_alpha;
-#define AMDM37X_DISPC_GLOBAL_ALPHA_GFXGLOBALALPHA_MASK  0xff
-#define AMDM37X_DISPC_GLOBAL_ALPHA_GFXGLOBALALPHA_SHIFT  0
-#define AMDM37X_DISPC_GLOBAL_ALPHA_VID2GLOBALALPHA_MASK  0xff
-#define AMDM37X_DISPC_GLOBAL_ALPHA_VID2GLOBALALPHA_SHIFT  16
-
-	ioport32_t size_dig;
-	ioport32_t size_lcd;
-
-	struct {
-		ioport32_t ba[2];
-		ioport32_t position;
-#define AMDM37X_DISPC_GFX_POSITION_GFXPOSX_MASK  0x7ff
-#define AMDM37X_DISPC_GFX_POSITION_GFXPOSX_SHIFT  0
-#define AMDM37X_DISPC_GFX_POSITION_GFXPOSY_MASK  0x7ff
-#define AMDM37X_DISPC_GFX_POSITION_GFXPOSY_SHIFT  16
-
-		ioport32_t size;
-#define AMDM37X_DISPC_SIZE_WIDTH_MASK  0x7ff
-#define AMDM37X_DISPC_SIZE_WIDTH_SHIFT  0
-#define AMDM37X_DISPC_SIZE_HEIGHT_MASK  0x7ff
-#define AMDM37X_DISPC_SIZE_HEIGHT_SHIFT  16
-
-		PADD32(4);
-		ioport32_t attributes;
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_ENABLE_FLAG  (1 << 0)
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_FORMAT_MASK  0xf
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_FORMAT_SHIFT  1
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_FORMAT_ARGB16  0x5
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_FORMAT_RGB16  0x6
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_FORMAT_RGB24_32  0x8
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_FORMAT_RGB24  0x9
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_FORMAT_ARGB  0xc
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_FORMAT_RGBA  0xd
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_FORMAT_RGBX  0xe
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_REPLICATIONENABLE_FLAG  (1 << 5)
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_GFXBURSTSIZE_MASK  0x3
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_GFXBURSTSIZE_SHIFT  6
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_GFXCHANNELOUT_FLAG  (1 << 8)
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_GFXNIBBLEMODE_FLAG  (1 << 9)
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_GFXENDIANNES_FLAG  (1 << 10)
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_GFXFIFOPRELOAD_FLAG  (1 << 11)
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_GFXROTATION_MASK  0x3
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_GFXROTATION_SHIFT  12
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_GFXARBITRATION_FLAG  (1 << 14)
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_GFXSELFREFRESH_FLAG  (1 << 15)
-#define AMDM37X_DISPC_GFX_ATTRIBUTES_PREMULTIALPHA_FLAG  (1 << 28)
-
-
-		ioport32_t fifo_threshold;
-		const ioport32_t fifo_size_status;
-		ioport32_t row_inc;
-		ioport32_t pixel_inc;
-		ioport32_t window_skip;
-		ioport32_t table_ba;
-	} gfx;
-
-	struct {
-		ioport32_t ba[2];
-		ioport32_t position;
-		ioport32_t size;
-		ioport32_t attributes;
-		ioport32_t fifo_threshold;
-		const ioport32_t fifo_size_status;
-		ioport32_t row_inc;
-		ioport32_t pixel_inc;
-		ioport32_t fir;
-		ioport32_t picture_size;
-		ioport32_t accui[2];
-		struct {
-			ioport32_t hi;
-			ioport32_t hvi;
-		} fir_coef[8];
-		ioport32_t conv_coef[5];
-		PADD32(2);
-	} vid[2];
-	/* 0x1d4 */
-	ioport32_t data_cycle[3];
-	/* 0x1e0 */
-	ioport32_t vid_fir_coef_v[8];
-	/* 0x200 */
-	PADD32(8);
-	/* 0x220 */
-	ioport32_t cpr_coef_r;
-	ioport32_t cpr_coef_g;
-	ioport32_t cpr_coef_b;
-	ioport32_t gfx_preload;
-
-	/* 0x230 */
-	ioport32_t vid_preload[2];
-
-} __attribute__((packed)) amdm37x_dispc_regs_t;
-
-
-static inline void amdm37x_dispc_setup_fb(amdm37x_dispc_regs_t *regs,
-    unsigned x, unsigned y, unsigned bpp, uintptr_t pa)
-{
-	ASSERT(regs);
-	/* Init sequence for dispc is in chapter 7.6.5.1.4 p. 1810,
-	 * no idea what parts of that work. */
-
-	/* Disable all interrupts */
-	regs->irqenable = 0;
-
-	/* Pixel format specifics*/
-	uint32_t attrib_pixel_format = 0;
-	uint32_t control_data_lanes = 0;
-	switch (bpp)
-	{
-	case 32:
-		attrib_pixel_format = AMDM37X_DISPC_GFX_ATTRIBUTES_FORMAT_RGBX;
-		control_data_lanes = AMDM37X_DISPC_CONTROL_TFTDATALINES_24B;
-		break;
-	case 24:
-		attrib_pixel_format = AMDM37X_DISPC_GFX_ATTRIBUTES_FORMAT_RGB24;
-		control_data_lanes = AMDM37X_DISPC_CONTROL_TFTDATALINES_24B;
-		break;
-	case 16:
-		attrib_pixel_format = AMDM37X_DISPC_GFX_ATTRIBUTES_FORMAT_RGB16;
-		control_data_lanes = AMDM37X_DISPC_CONTROL_TFTDATALINES_16B;
-		break;
-	default:
-		ASSERT(false);
-	}
-
-	/* Prepare sizes */
-	const uint32_t size_reg =
-	    (((x - 1) & AMDM37X_DISPC_SIZE_WIDTH_MASK)
-	        << AMDM37X_DISPC_SIZE_WIDTH_SHIFT) |
-	    (((y - 1) & AMDM37X_DISPC_SIZE_HEIGHT_MASK)
-	        << AMDM37X_DISPC_SIZE_HEIGHT_SHIFT);
-
-	/* modes taken from u-boot, for 1024x768 */
-	// TODO replace magic values with actual correct values
-//	regs->timing_h = 0x1a4024c9;
-//	regs->timing_v = 0x02c00509;
-//	regs->pol_freq = 0x00007028;
-//	regs->divisor  = 0x00010001;
-
-	/* setup output */
-	regs->size_lcd = size_reg;
-	regs->size_dig = size_reg;
-
-	/* Nice blue default color */
-	regs->default_color[0] = 0x0000ff;
-	regs->default_color[1] = 0x0000ff;
-
-	/* Setup control register */
-	uint32_t control = 0 |
-		AMDM37X_DISPC_CONTROL_PCKFREEENABLE_FLAG |
-		(control_data_lanes << AMDM37X_DISPC_CONTROL_TFTDATALINES_SHIFT) |
-		AMDM37X_DISPC_CONTROL_GPOUT0_FLAG |
-		AMDM37X_DISPC_CONTROL_GPOUT1_FLAG;
-	regs->control = control;
-
-	/* No gamma stuff only data */
-	uint32_t config = (AMDM37X_DISPC_CONFIG_LOADMODE_DATAEVERYFRAME
-	            << AMDM37X_DISPC_CONFIG_LOADMODE_SHIFT);
-	regs->config = config;
-
-
-	/* Set framebuffer base address */
-	regs->gfx.ba[0] = pa;
-	regs->gfx.ba[1] = pa;
-	regs->gfx.position = 0;
-
-	/* Setup fb size */
-	regs->gfx.size = size_reg;
-
-	/* Set pixel format */
-	uint32_t attribs = 0 |
-	    (attrib_pixel_format << AMDM37X_DISPC_GFX_ATTRIBUTES_FORMAT_SHIFT);
-	regs->gfx.attributes = attribs;
-
-	/* 0x03ff03c0 is the default */
-	regs->gfx.fifo_threshold = 0x03ff03c0;
-	/* This value should be stride - width, 1 means next pixel i.e.
-	 * stride == width */
-	regs->gfx.row_inc = 1;
-	/* number of bytes to next pixel in BPP multiples */
-	regs->gfx.pixel_inc = 1;
-	/* only used if video is played over fb */
-	regs->gfx.window_skip = 0;
-	/* Gamma and palette table */
-	regs->gfx.table_ba = 0;
-
-	/* enable frame buffer graphics */
-	regs->gfx.attributes |= AMDM37X_DISPC_GFX_ATTRIBUTES_ENABLE_FLAG;
-	/* Update register values */
-	regs->control |= AMDM37X_DISPC_CONTROL_GOLCD_FLAG;
-	regs->control |= AMDM37X_DISPC_CONTROL_GODIGITAL_FLAG;
-	/* Enable output */
-	regs->control |= AMDM37X_DISPC_CONTROL_LCD_ENABLE_FLAG;
-	regs->control |= AMDM37X_DISPC_CONTROL_DIGITAL_ENABLE_FLAG;
-}
-
-
-#endif
-/**
- * @}
- */
Index: uspace/app/netecho/netecho.c
===================================================================
--- uspace/app/netecho/netecho.c	(revision 8fe2c9bdbf4cd735969137d973f2ce061bae7aa9)
+++ uspace/app/netecho/netecho.c	(revision 119b46e45d903caf31b28eb1f2046fe108aa20c4)
@@ -243,5 +243,5 @@
 		if (verbose)
 			printf("accept()\n");
-            	socket_id = accept(listening_id, (void *) address_buf, &addrlen);
+		socket_id = accept(listening_id, (void *) address_buf, &addrlen);
 		if (socket_id <= 0) {
 			socket_print_error(stderr, socket_id, "Socket accept: ", "\n");
@@ -312,5 +312,5 @@
 				rc = sendto(socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen);
 				if (rc != EOK)
-					socket_print_error(stderr, rc, "Socket send: ", "\n");
+					socket_print_error(stderr, rc, "Socket sendto: ", "\n");
 			}
 		}
@@ -394,5 +394,12 @@
 		return listening_id;
 	}
-
+	
+	/* Bind the listening socket */
+	rc = bind(listening_id, address, addrlen);
+	if (rc != EOK) {
+		socket_print_error(stderr, rc, "Socket bind: ", "\n");
+		return rc;
+	}
+	
 	/* if the stream socket is used */
 	if (type == SOCK_STREAM) {
@@ -402,5 +409,5 @@
 			backlog = 3;
 		}
-
+		
 		/* Set the backlog */
 		rc = listen(listening_id, backlog);
@@ -409,11 +416,4 @@
 			return rc;
 		}
-	}
-
-	/* Bind the listening socket */
-	rc = bind(listening_id, address, addrlen);
-	if (rc != EOK) {
-		socket_print_error(stderr, rc, "Socket bind: ", "\n");
-		return rc;
 	}
 
Index: uspace/app/nettest3/nettest3.c
===================================================================
--- uspace/app/nettest3/nettest3.c	(revision 8fe2c9bdbf4cd735969137d973f2ce061bae7aa9)
+++ uspace/app/nettest3/nettest3.c	(revision 119b46e45d903caf31b28eb1f2046fe108aa20c4)
@@ -63,5 +63,5 @@
 	port = 7;
 
-	data = (char *)"Hello World!";
+	data = (char *) "Hello World!";
 	size = str_size(data);
 
@@ -98,5 +98,5 @@
 
 	printf("connect()\n");
-	rc = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
+	rc = connect(fd, (struct sockaddr *) &addr, sizeof(addr));
 	printf(" -> %d\n", rc);
 	if (rc != 0)
@@ -115,5 +115,5 @@
 	} while (rc > 0);
 
-	async_usleep(1000*1000);
+	async_usleep(1000 * 1000);
 
 	printf("closesocket()\n");
@@ -124,5 +124,4 @@
 }
 
-
 /** @}
  */
Index: uspace/lib/c/generic/async.c
===================================================================
--- uspace/lib/c/generic/async.c	(revision 8fe2c9bdbf4cd735969137d973f2ce061bae7aa9)
+++ uspace/lib/c/generic/async.c	(revision 119b46e45d903caf31b28eb1f2046fe108aa20c4)
@@ -2068,4 +2068,5 @@
 	
 	async_sess_t *sess = exch->sess;
+	assert(sess != NULL);
 	
 	atomic_dec(&sess->refcnt);
Index: uspace/lib/c/generic/net/socket_client.c
===================================================================
--- uspace/lib/c/generic/net/socket_client.c	(revision 8fe2c9bdbf4cd735969137d973f2ce061bae7aa9)
+++ uspace/lib/c/generic/net/socket_client.c	(revision 119b46e45d903caf31b28eb1f2046fe108aa20c4)
@@ -192,84 +192,88 @@
 /** Default thread for new connections.
  *
- * @param[in] iid	The initial message identifier.
- * @param[in] icall	The initial message call structure.
- * @param[in] arg	Local argument.
+ * @param[in] iid   The initial message identifier.
+ * @param[in] icall The initial message call structure.
+ * @param[in] arg   Local argument.
+ *
  */
 static void socket_connection(ipc_callid_t iid, ipc_call_t * icall, void *arg)
 {
-	ipc_callid_t callid;
-	ipc_call_t call;
-	socket_t *socket;
-	int rc;
-
-loop:
-	callid = async_get_call(&call);
-
-	switch (IPC_GET_IMETHOD(call)) {
-	case NET_SOCKET_RECEIVED:
-	case NET_SOCKET_ACCEPTED:
-	case NET_SOCKET_DATA_FRAGMENT_SIZE:
-		fibril_rwlock_read_lock(&socket_globals.lock);
-
-		/* Find the socket */
-		socket = sockets_find(socket_get_sockets(),
-		    SOCKET_GET_SOCKET_ID(call));
-		if (!socket) {
-			rc = ENOTSOCK;
-			fibril_rwlock_read_unlock(&socket_globals.lock);
-			break;
+	while (true) {
+		ipc_call_t call;
+		ipc_callid_t callid = async_get_call(&call);
+		
+		if (!IPC_GET_IMETHOD(call)) {
+			async_answer_0(callid, 0);
+			return;
 		}
+		
+		int rc;
 		
 		switch (IPC_GET_IMETHOD(call)) {
 		case NET_SOCKET_RECEIVED:
-			fibril_mutex_lock(&socket->receive_lock);
-			/* Push the number of received packet fragments */
-			rc = dyn_fifo_push(&socket->received,
-			    SOCKET_GET_DATA_FRAGMENTS(call),
-			    SOCKET_MAX_RECEIVED_SIZE);
-			if (rc == EOK) {
-				/* Signal the received packet */
-				fibril_condvar_signal(&socket->receive_signal);
+		case NET_SOCKET_ACCEPTED:
+		case NET_SOCKET_DATA_FRAGMENT_SIZE:
+			fibril_rwlock_read_lock(&socket_globals.lock);
+			
+			/* Find the socket */
+			socket_t *socket = sockets_find(socket_get_sockets(),
+			    SOCKET_GET_SOCKET_ID(call));
+			if (!socket) {
+				rc = ENOTSOCK;
+				fibril_rwlock_read_unlock(&socket_globals.lock);
+				break;
 			}
-			fibril_mutex_unlock(&socket->receive_lock);
+			
+			switch (IPC_GET_IMETHOD(call)) {
+			case NET_SOCKET_RECEIVED:
+				fibril_mutex_lock(&socket->receive_lock);
+				/* Push the number of received packet fragments */
+				rc = dyn_fifo_push(&socket->received,
+				    SOCKET_GET_DATA_FRAGMENTS(call),
+				    SOCKET_MAX_RECEIVED_SIZE);
+				if (rc == EOK) {
+					/* Signal the received packet */
+					fibril_condvar_signal(&socket->receive_signal);
+				}
+				fibril_mutex_unlock(&socket->receive_lock);
+				break;
+				
+			case NET_SOCKET_ACCEPTED:
+				/* Push the new socket identifier */
+				fibril_mutex_lock(&socket->accept_lock);
+				rc = dyn_fifo_push(&socket->accepted, 1,
+				    SOCKET_MAX_ACCEPTED_SIZE);
+				if (rc == EOK) {
+					/* Signal the accepted socket */
+					fibril_condvar_signal(&socket->accept_signal);
+				}
+				fibril_mutex_unlock(&socket->accept_lock);
+				break;
+			
+			default:
+				rc = ENOTSUP;
+			}
+			
+			if ((SOCKET_GET_DATA_FRAGMENT_SIZE(call) > 0) &&
+			    (SOCKET_GET_DATA_FRAGMENT_SIZE(call) !=
+			    socket->data_fragment_size)) {
+				fibril_rwlock_write_lock(&socket->sending_lock);
+				
+				/* Set the data fragment size */
+				socket->data_fragment_size =
+				    SOCKET_GET_DATA_FRAGMENT_SIZE(call);
+				
+				fibril_rwlock_write_unlock(&socket->sending_lock);
+			}
+			
+			fibril_rwlock_read_unlock(&socket_globals.lock);
 			break;
-
-		case NET_SOCKET_ACCEPTED:
-			/* Push the new socket identifier */
-			fibril_mutex_lock(&socket->accept_lock);
-			rc = dyn_fifo_push(&socket->accepted, 1,
-			    SOCKET_MAX_ACCEPTED_SIZE);
-			if (rc == EOK) {
-				/* Signal the accepted socket */
-				fibril_condvar_signal(&socket->accept_signal);
-			}
-			fibril_mutex_unlock(&socket->accept_lock);
-			break;
-
+			
 		default:
 			rc = ENOTSUP;
 		}
-
-		if ((SOCKET_GET_DATA_FRAGMENT_SIZE(call) > 0) &&
-		    (SOCKET_GET_DATA_FRAGMENT_SIZE(call) !=
-		    socket->data_fragment_size)) {
-			fibril_rwlock_write_lock(&socket->sending_lock);
-
-			/* Set the data fragment size */
-			socket->data_fragment_size =
-			    SOCKET_GET_DATA_FRAGMENT_SIZE(call);
-
-			fibril_rwlock_write_unlock(&socket->sending_lock);
-		}
-
-		fibril_rwlock_read_unlock(&socket_globals.lock);
-		break;
-
-	default:
-		rc = ENOTSUP;
-	}
-
-	async_answer_0(callid, (sysarg_t) rc);
-	goto loop;
+		
+		async_answer_0(callid, (sysarg_t) rc);
+	}
 }
 
Index: uspace/srv/net/tcp/conn.c
===================================================================
--- uspace/srv/net/tcp/conn.c	(revision 8fe2c9bdbf4cd735969137d973f2ce061bae7aa9)
+++ uspace/srv/net/tcp/conn.c	(revision 119b46e45d903caf31b28eb1f2046fe108aa20c4)
@@ -354,13 +354,19 @@
 {
 	log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_find_ref(%p)", sp);
-
+	
+	log_msg(LOG_DEFAULT, LVL_DEBUG2, "compare conn (f:(%x,%u), l:(%x,%u))",
+	    sp->foreign.addr.ipv4, sp->foreign.port,
+	    sp->local.addr.ipv4, sp->local.port);
+	
 	fibril_mutex_lock(&conn_list_lock);
-
+	
 	list_foreach(conn_list, link) {
 		tcp_conn_t *conn = list_get_instance(link, tcp_conn_t, link);
 		tcp_sockpair_t *csp = &conn->ident;
-		log_msg(LOG_DEFAULT, LVL_DEBUG2, "compare with conn (f:(%x,%u), l:(%x,%u))",
+		
+		log_msg(LOG_DEFAULT, LVL_DEBUG2, " - with (f:(%x,%u), l:(%x,%u))",
 		    csp->foreign.addr.ipv4, csp->foreign.port,
 		    csp->local.addr.ipv4, csp->local.port);
+		
 		if (tcp_sockpair_match(sp, csp)) {
 			tcp_conn_addref(conn);
@@ -369,5 +375,5 @@
 		}
 	}
-
+	
 	fibril_mutex_unlock(&conn_list_lock);
 	return NULL;
Index: uspace/srv/net/tcp/sock.c
===================================================================
--- uspace/srv/net/tcp/sock.c	(revision 8fe2c9bdbf4cd735969137d973f2ce061bae7aa9)
+++ uspace/srv/net/tcp/sock.c	(revision 119b46e45d903caf31b28eb1f2046fe108aa20c4)
@@ -203,36 +203,51 @@
 {
 	int rc;
-	struct sockaddr *addr;
-	size_t addr_len;
+	struct sockaddr_in *addr;
+	size_t addr_size;
 	socket_core_t *sock_core;
 	tcp_sockdata_t *socket;
-
+	
 	log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_bind()");
 	log_msg(LOG_DEFAULT, LVL_DEBUG, " - async_data_write_accept");
-	rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, &addr_len);
+	
+	addr = NULL;
+	
+	rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, &addr_size);
 	if (rc != EOK) {
 		async_answer_0(callid, rc);
-		return;
-	}
-
+		goto out;
+	}
+	
+	if (addr_size != sizeof(struct sockaddr_in)) {
+		async_answer_0(callid, EINVAL);
+		goto out;
+	}
+	
 	log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_bind");
 	rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call),
-	    addr, addr_len, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
+	    addr, addr_size, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
 	    last_used_port);
 	if (rc != EOK) {
 		async_answer_0(callid, rc);
-		return;
-	}
-
+		goto out;
+	}
+	
 	log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_cores_find");
 	sock_core = socket_cores_find(&client->sockets, SOCKET_GET_SOCKET_ID(call));
-	if (sock_core != NULL) {
-		socket = (tcp_sockdata_t *)sock_core->specific_data;
-		/* XXX Anything to do? */
-		(void) socket;
-	}
-
+	if (sock_core == NULL) {
+		async_answer_0(callid, ENOENT);
+		goto out;
+	}
+	
+	socket = (tcp_sockdata_t *)sock_core->specific_data;
+	/* XXX Anything to do? */
+	(void) socket;
+	
 	log_msg(LOG_DEFAULT, LVL_DEBUG, " - success");
 	async_answer_0(callid, EOK);
+	
+out:
+	if (addr != NULL)
+		free(addr);
 }
 
@@ -249,4 +264,5 @@
 	tcp_sock_lconn_t *lconn;
 	int i;
+	int rc;
 
 	log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_listen()");
@@ -268,12 +284,24 @@
 		return;
 	}
-
-	socket = (tcp_sockdata_t *)sock_core->specific_data;
-
+	
+	if (sock_core->port <= 0) {
+		rc = socket_bind_free_port(&gsock, sock_core,
+		    TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
+		    last_used_port);
+		if (rc != EOK) {
+			async_answer_0(callid, rc);
+			return;
+		}
+		
+		last_used_port = sock_core->port;
+	}
+	
+	socket = (tcp_sockdata_t *) sock_core->specific_data;
+	
 	/*
 	 * Prepare @c backlog listening connections.
 	 */
 	fibril_mutex_lock(&socket->lock);
-
+	
 	socket->backlog = backlog;
 	socket->lconn = calloc(backlog, sizeof(tcp_conn_t *));
@@ -283,12 +311,12 @@
 		return;
 	}
-
+	
 	log_msg(LOG_DEFAULT, LVL_DEBUG, " - open connections");
-
+	
 	lsocket.addr.ipv4 = TCP_IPV4_ANY;
 	lsocket.port = sock_core->port;
 	fsocket.addr.ipv4 = TCP_IPV4_ANY;
 	fsocket.port = TCP_PORT_ANY;
-
+	
 	for (i = 0; i < backlog; i++) {
 
@@ -362,5 +390,5 @@
 			return;
 		}
-
+		
 		last_used_port = sock_core->port;
 	}
@@ -441,4 +469,16 @@
 		return;
 	}
+	
+	if (sock_core->port <= 0) {
+		rc = socket_bind_free_port(&gsock, sock_core,
+		    TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
+		    last_used_port);
+		if (rc != EOK) {
+			async_answer_0(callid, rc);
+			return;
+		}
+		
+		last_used_port = sock_core->port;
+	}
 
 	socket = (tcp_sockdata_t *)sock_core->specific_data;
Index: uspace/srv/net/udp/assoc.c
===================================================================
--- uspace/srv/net/udp/assoc.c	(revision 8fe2c9bdbf4cd735969137d973f2ce061bae7aa9)
+++ uspace/srv/net/udp/assoc.c	(revision 119b46e45d903caf31b28eb1f2046fe108aa20c4)
@@ -200,6 +200,7 @@
 /** Set local socket in association.
  *
- * @param assoc		Association
- * @param fsock		Foreign socket (deeply copied)
+ * @param assoc Association
+ * @param lsock Local socket (deeply copied)
+ *
  */
 void udp_assoc_set_local(udp_assoc_t *assoc, udp_sock_t *lsock)
@@ -208,4 +209,18 @@
 	fibril_mutex_lock(&assoc->lock);
 	assoc->ident.local = *lsock;
+	fibril_mutex_unlock(&assoc->lock);
+}
+
+/** Set local port in association.
+ *
+ * @param assoc Association
+ * @param lport Local port
+ *
+ */
+void udp_assoc_set_local_port(udp_assoc_t *assoc, uint16_t lport)
+{
+	log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_set_local(%p, %" PRIu16 ")", assoc, lport);
+	fibril_mutex_lock(&assoc->lock);
+	assoc->ident.local.port = lport;
 	fibril_mutex_unlock(&assoc->lock);
 }
@@ -406,5 +421,4 @@
 }
 
-
 /**
  * @}
Index: uspace/srv/net/udp/assoc.h
===================================================================
--- uspace/srv/net/udp/assoc.h	(revision 8fe2c9bdbf4cd735969137d973f2ce061bae7aa9)
+++ uspace/srv/net/udp/assoc.h	(revision 119b46e45d903caf31b28eb1f2046fe108aa20c4)
@@ -47,4 +47,5 @@
 extern void udp_assoc_set_foreign(udp_assoc_t *, udp_sock_t *);
 extern void udp_assoc_set_local(udp_assoc_t *, udp_sock_t *);
+extern void udp_assoc_set_local_port(udp_assoc_t *, uint16_t);
 extern int udp_assoc_send(udp_assoc_t *, udp_sock_t *, udp_msg_t *);
 extern int udp_assoc_recv(udp_assoc_t *, udp_msg_t **, udp_sock_t *);
Index: uspace/srv/net/udp/sock.c
===================================================================
--- uspace/srv/net/udp/sock.c	(revision 8fe2c9bdbf4cd735969137d973f2ce061bae7aa9)
+++ uspace/srv/net/udp/sock.c	(revision 119b46e45d903caf31b28eb1f2046fe108aa20c4)
@@ -90,5 +90,5 @@
 	log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_notify_data(%d)", sock_core->socket_id);
 	async_exch_t *exch = async_exchange_begin(sock_core->sess);
-	async_msg_5(exch, NET_SOCKET_RECEIVED, (sysarg_t)sock_core->socket_id,
+	async_msg_5(exch, NET_SOCKET_RECEIVED, (sysarg_t) sock_core->socket_id,
 	    UDP_FRAGMENT_SIZE, 0, 0, 1);
 	async_exchange_end(exch);
@@ -177,5 +177,10 @@
 		goto out;
 	}
-
+	
+	if (addr_size != sizeof(struct sockaddr_in)) {
+		async_answer_0(callid, EINVAL);
+		goto out;
+	}
+	
 	log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_bind");
 	rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call),
@@ -186,10 +191,5 @@
 		goto out;
 	}
-
-	if (addr_size != sizeof(struct sockaddr_in)) {
-		async_answer_0(callid, EINVAL);
-		goto out;
-	}
-
+	
 	log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_cores_find");
 	sock_core = socket_cores_find(&client->sockets, SOCKET_GET_SOCKET_ID(call));
@@ -249,25 +249,13 @@
 static void udp_sock_sendto(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
 {
-	int socket_id;
-	int fragments;
-	int index;
-	struct sockaddr_in *addr;
-	size_t addr_size;
-	socket_core_t *sock_core;
-	udp_sockdata_t *socket;
-	udp_sock_t fsock, *fsockp;
-	ipc_call_t answer;
-	ipc_callid_t wcallid;
-	size_t length;
-	uint8_t buffer[UDP_FRAGMENT_SIZE];
-	udp_error_t urc;
-	int rc;
-
 	log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_send()");
-
-	addr = NULL;
-
+	
+	struct sockaddr_in *addr = NULL;
+	udp_sock_t fsock;
+	udp_sock_t *fsock_ptr;
+	
 	if (IPC_GET_IMETHOD(call) == NET_SOCKET_SENDTO) {
-		rc = async_data_write_accept((void **) &addr, false,
+		size_t addr_size;
+		int rc = async_data_write_accept((void **) &addr, false,
 		    0, 0, 0, &addr_size);
 		if (rc != EOK) {
@@ -275,49 +263,63 @@
 			goto out;
 		}
-
+		
 		if (addr_size != sizeof(struct sockaddr_in)) {
 			async_answer_0(callid, EINVAL);
 			goto out;
 		}
-
+		
 		fsock.addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr);
 		fsock.port = uint16_t_be2host(addr->sin_port);
-		fsockp = &fsock;
-	} else {
-		fsockp = NULL;
-	}
-
-	socket_id = SOCKET_GET_SOCKET_ID(call);
-	fragments = SOCKET_GET_DATA_FRAGMENTS(call);
+		fsock_ptr = &fsock;
+	} else
+		fsock_ptr = NULL;
+	
+	int socket_id = SOCKET_GET_SOCKET_ID(call);
+	
 	SOCKET_GET_FLAGS(call);
-
-	sock_core = socket_cores_find(&client->sockets, socket_id);
+	
+	socket_core_t *sock_core =
+	    socket_cores_find(&client->sockets, socket_id);
 	if (sock_core == NULL) {
 		async_answer_0(callid, ENOTSOCK);
 		goto out;
 	}
-
-	if (sock_core->port == 0) {
+	
+	udp_sockdata_t *socket =
+	    (udp_sockdata_t *) sock_core->specific_data;
+	
+	if (sock_core->port <= 0) {
 		/* Implicitly bind socket to port */
-		rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call),
-		    addr, addr_size, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
-		    last_used_port);
+		int rc = socket_bind_free_port(&gsock, sock_core,
+		    UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, last_used_port);
 		if (rc != EOK) {
 			async_answer_0(callid, rc);
 			goto out;
 		}
-	}
-
-	socket = (udp_sockdata_t *)sock_core->specific_data;
+		
+		assert(sock_core->port > 0);
+		
+		udp_error_t urc = udp_uc_set_local_port(socket->assoc,
+		    sock_core->port);
+		
+		if (urc != UDP_EOK) {
+			// TODO: better error handling
+			async_answer_0(callid, EINTR);
+			goto out;
+		}
+		
+		last_used_port = sock_core->port;
+	}
+	
 	fibril_mutex_lock(&socket->lock);
-
+	
 	if (socket->assoc->ident.local.addr.ipv4 == UDP_IPV4_ANY) {
 		/* Determine local IP address */
 		inet_addr_t loc_addr, rem_addr;
-
-		rem_addr.ipv4 = fsockp ? fsock.addr.ipv4 :
+		
+		rem_addr.ipv4 = fsock_ptr ? fsock.addr.ipv4 :
 		    socket->assoc->ident.foreign.addr.ipv4;
-
-		rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr);
+		
+		int rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr);
 		if (rc != EOK) {
 			fibril_mutex_unlock(&socket->lock);
@@ -327,14 +329,17 @@
 			return;
 		}
-
+		
 		socket->assoc->ident.local.addr.ipv4 = loc_addr.ipv4;
 		log_msg(LOG_DEFAULT, LVL_DEBUG, "Local IP address is %x",
 		    socket->assoc->ident.local.addr.ipv4);
 	}
-
-
+	
 	assert(socket->assoc != NULL);
-
-	for (index = 0; index < fragments; index++) {
+	
+	int fragments = SOCKET_GET_DATA_FRAGMENTS(call);
+	for (int index = 0; index < fragments; index++) {
+		ipc_callid_t wcallid;
+		size_t length;
+		
 		if (!async_data_write_receive(&wcallid, &length)) {
 			fibril_mutex_unlock(&socket->lock);
@@ -342,9 +347,10 @@
 			goto out;
 		}
-
+		
 		if (length > UDP_FRAGMENT_SIZE)
 			length = UDP_FRAGMENT_SIZE;
-
-		rc = async_data_write_finalize(wcallid, buffer, length);
+		
+		uint8_t buffer[UDP_FRAGMENT_SIZE];
+		int rc = async_data_write_finalize(wcallid, buffer, length);
 		if (rc != EOK) {
 			fibril_mutex_unlock(&socket->lock);
@@ -352,24 +358,25 @@
 			goto out;
 		}
-
-		urc = udp_uc_send(socket->assoc, fsockp, buffer, length, 0);
-
+		
+		udp_error_t urc =
+		    udp_uc_send(socket->assoc, fsock_ptr, buffer, length, 0);
+		
 		switch (urc) {
 		case UDP_EOK:
 			rc = EOK;
 			break;
-/*		case TCP_ENOTEXIST:
-			rc = ENOTCONN;
-			break;
-		case TCP_ECLOSING:
-			rc = ENOTCONN;
-			break;
-		case TCP_ERESET:
-			rc = ECONNABORTED;
-			break;*/
+		case UDP_ENORES:
+			rc = ENOMEM;
+			break;
+		case UDP_EUNSPEC:
+			rc = EINVAL;
+			break;
+		case UDP_ENOROUTE:
+			rc = EIO;
+			break;
 		default:
 			assert(false);
 		}
-
+		
 		if (rc != EOK) {
 			fibril_mutex_unlock(&socket->lock);
@@ -378,4 +385,6 @@
 		}
 	}
+	
+	ipc_call_t answer;
 	
 	IPC_SET_ARG1(answer, 0);
@@ -526,22 +535,19 @@
 static void udp_sock_close(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
 {
-	int socket_id;
-	socket_core_t *sock_core;
-	udp_sockdata_t *socket;
-	int rc;
-
 	log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close()");
-	socket_id = SOCKET_GET_SOCKET_ID(call);
-
-	sock_core = socket_cores_find(&client->sockets, socket_id);
+	int socket_id = SOCKET_GET_SOCKET_ID(call);
+	
+	socket_core_t *sock_core =
+	    socket_cores_find(&client->sockets, socket_id);
 	if (sock_core == NULL) {
 		async_answer_0(callid, ENOTSOCK);
 		return;
 	}
-
-	socket = (udp_sockdata_t *)sock_core->specific_data;
+	
+	udp_sockdata_t *socket =
+	    (udp_sockdata_t *) sock_core->specific_data;
 	fibril_mutex_lock(&socket->lock);
-
-	rc = socket_destroy(NULL, socket_id, &client->sockets, &gsock,
+	
+	int rc = socket_destroy(NULL, socket_id, &client->sockets, &gsock,
 	    udp_free_sock_data);
 	if (rc != EOK) {
@@ -550,5 +556,5 @@
 		return;
 	}
-
+	
 	fibril_mutex_unlock(&socket->lock);
 	async_answer_0(callid, EOK);
@@ -583,12 +589,12 @@
 			    &sock->recv_buffer_lock);
 		}
-
+		
 		log_msg(LOG_DEFAULT, LVL_DEBUG, "[] call udp_uc_receive()");
 		urc = udp_uc_receive(sock->assoc, sock->recv_buffer,
 		    UDP_FRAGMENT_SIZE, &rcvd, &xflags, &sock->recv_fsock);
 		sock->recv_error = urc;
-
+		
 		udp_sock_notify_data(sock->sock_core);
-
+		
 		if (urc != UDP_EOK) {
 			fibril_condvar_broadcast(&sock->recv_buffer_cv);
@@ -596,7 +602,7 @@
 			break;
 		}
-
+		
 		log_msg(LOG_DEFAULT, LVL_DEBUG, "[] got data - broadcast recv_buffer_cv");
-
+		
 		sock->recv_buffer_used = rcvd;
 		fibril_mutex_unlock(&sock->recv_buffer_lock);
Index: uspace/srv/net/udp/ucall.c
===================================================================
--- uspace/srv/net/udp/ucall.c	(revision 8fe2c9bdbf4cd735969137d973f2ce061bae7aa9)
+++ uspace/srv/net/udp/ucall.c	(revision 119b46e45d903caf31b28eb1f2046fe108aa20c4)
@@ -68,6 +68,14 @@
 {
 	log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_uc_set_local(%p, %p)", assoc, lsock);
+	
+	udp_assoc_set_local(assoc, lsock);
+	return UDP_EOK;
+}
 
-	udp_assoc_set_local(assoc, lsock);
+udp_error_t udp_uc_set_local_port(udp_assoc_t *assoc, uint16_t lport)
+{
+	log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_uc_set_local(%p, %" PRIu16 ")", assoc, lport);
+	
+	udp_assoc_set_local_port(assoc, lport);
 	return UDP_EOK;
 }
Index: uspace/srv/net/udp/ucall.h
===================================================================
--- uspace/srv/net/udp/ucall.h	(revision 8fe2c9bdbf4cd735969137d973f2ce061bae7aa9)
+++ uspace/srv/net/udp/ucall.h	(revision 119b46e45d903caf31b28eb1f2046fe108aa20c4)
@@ -42,4 +42,5 @@
 extern udp_error_t udp_uc_set_foreign(udp_assoc_t *, udp_sock_t *);
 extern udp_error_t udp_uc_set_local(udp_assoc_t *, udp_sock_t *);
+extern udp_error_t udp_uc_set_local_port(udp_assoc_t *, uint16_t);
 extern udp_error_t udp_uc_send(udp_assoc_t *, udp_sock_t *, void *, size_t,
     xflags_t);
Index: uspace/srv/net/udp/udp_type.h
===================================================================
--- uspace/srv/net/udp/udp_type.h	(revision 8fe2c9bdbf4cd735969137d973f2ce061bae7aa9)
+++ uspace/srv/net/udp/udp_type.h	(revision 119b46e45d903caf31b28eb1f2046fe108aa20c4)
@@ -66,5 +66,5 @@
 };
 
-enum tcp_port {
+enum udp_port {
 	UDP_PORT_ANY = 0
 };
